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
                }
            })
            
        }
    }
}
5
  • 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. Commented Feb 2, 2021 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. Commented Sep 21, 2021 at 13:33
  • there is one solution stackoverflow.com/a/69484580/7576100 Commented Oct 7, 2021 at 16:19
  • For me this happened when I had the same Bool binding in two different NavigationLinks Commented Feb 20, 2022 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? Commented May 11, 2022 at 14:11

3 Answers 3

40

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()}
 )
Sign up to request clarification or add additional context in comments.

2 Comments

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
The data can be processed in the variable's setter 'selectedItem'.
3

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

1 Comment

this was it! thank you :) quicker than chatgpt ❣️
0
ForEach(items) { item in
            NavigationLink(tag: item.id, selection: $selection) {
                DetailView(selection: $selection, item: item)
            } label: {
                Text("\(item)")
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.