17

Console Bug: SwiftUI encountered an issue when pushing aNavigationLink. Please file a bug.

There is no problem when I don't use the isActive parameter in NavigationLink. However, I have to use the isActive parameter. Because I'm closing the drop-down list accordingly.

Menu Model:

struct Menu: Identifiable {
    var id: Int
    var pageName: String
    var icon: String
    var page: Any
    var startDelay: Double
    var endDelay: Double
//    var offsetY: CGFloat
}

let menu = [
    Menu(id: 1, pageName: "Profil", icon: "person.crop.circle", page: ProfileView(), startDelay: 0.2, endDelay: 0.6),
    Menu(id: 2, pageName: "Sepet", icon: "cart", page: CartView(), startDelay: 0.4, endDelay: 0.4),
    Menu(id: 3, pageName: "İstek", icon: "plus.circle", page: ClaimView(), startDelay: 0.6, endDelay: 0.2)
]

MenuView

struct MenuView: View {
    @State var isShownMenu: Bool = false
    @State var isPresented: Bool = false
    var body: some View {
        VStack(spacing: 40) {
            
            Button(action: {self.isShownMenu.toggle()}) {
                MenuViewButton(page: .constant((Any).self), icon: .constant("rectangle.stack"))
            }
            VStack(spacing: 40) {
                ForEach(menu, id: \.id) { item in
                    
                    NavigationLink(
                        destination: AnyView(_fromValue: item.page),
                        isActive: self.$isPresented,
                        label: {

                            MenuViewButton(page: .constant(item.page), icon: .constant(item.icon))
                        .animation(Animation.easeInOut.delay(self.isShownMenu ? item.startDelay : item.endDelay))
                        .offset(x: self.isShownMenu ? .zero : UIScreen.main.bounds.width)//, y: item.offsetY)
                }
            }
            .onChange(of: isPresented, perform: { value in
                if value == true {
                    self.isShownMenu = false
                }
            })
            
        }
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
Ufuk Köşker
  • 1,288
  • 8
  • 29
  • 9
    You have 3 (count of menu items) NavigationLink/s linked to 1 isPresented state property. Which one do you think should be activated? ... this is a bug of code logic. – Asperi Feb 02 '21 at 21:11
  • Also, Navigation in SwiftUI sucks. Anything this easy to break is just bad design. :( Avoid the use of the binder when at all possible, and dismiss via presentation mode in the destination. – ChrisH Sep 21 '21 at 13:33
  • there is one solution https://stackoverflow.com/a/69484580/7576100 – Jack Oct 07 '21 at 16:19
  • For me this happened when I had the same Bool binding in two different `NavigationLinks` – Big_Chair Feb 20 '22 at 19:13
  • I'm having the same problem now, but am only using `NavigationLink` without the `isActive` parameter. What else could be the problem? – DrMickeyLauer May 11 '22 at 14:11

3 Answers3

36

The problem is that you have NavigationLink with the "IsActive" parameter placed in the ForEach cycle! You need to remove NavigationLink from the cycle and transfer the necessary data there, for example, through the view model.

Summary: you should only have one NavigationLink associated with one specific isActive parameter.

 ForEach(yourData) { dataItem in
     Button {
         selectedItem = dataItem
         isActivated = true
     } label: {
         Text("\(dataItem)")            
     }
 }
 .background(
     NavigationLink(destination: DestinationView(data: selectedItem),
                    isActive: $isActivated) {EmptyView()}
 )
Sergei Volkov
  • 818
  • 8
  • 15
  • this is working for me but how are you gonna handle for example contente download in DestinationView? onAppear is being called just once, not everytime a DestinationView is pushed – laucel Sep 20 '21 at 12:12
  • The data can be processed in the variable's setter 'selectedItem'. – Sergei Volkov Sep 20 '21 at 16:11
1

I got this when I accidentally had two NavigationLinks with the same isActive boolean.

Russ
  • 467
  • 3
  • 10
0
ForEach(items) { item in
            NavigationLink(tag: item.id, selection: $selection) {
                DetailView(selection: $selection, item: item)
            } label: {
                Text("\(item)")
            }
        }