cbjeukendrup
cbjeukendrup

Reputation: 3446

SwiftUI: NavigationButton initializer gives error

Inside one of my SwiftUI Views, I have this code:

NavigationButton(destination: DetailView()) {
    Text(verbatim: "Start")
}

Xcode isn't able to build the code and gives this error:

'(DetailView, @escaping () -> Text) -> NavigationButton<Text, DetailView>' is not convertible to '(DetailView, Bool, @escaping () -> Bool, () -> Text) -> NavigationButton<Text, DetailView>'

I believe Xcode thinks that I am using the init(destination: Destination, isDetail: Bool, onTrigger: () -> Bool, label: () -> Label) initializer, but I am using the init(destination: Destination, label: () -> Label) one.

The strange thing is that the one I am using is not documented (https://developer.apple.com/documentation/swiftui/navigationbutton) but it is used in multiple tutorials I have seen, for example https://www.hackingwithswift.com/quick-start/swiftui/how-to-push-a-new-view-using-navigationbutton.

So my question is: Is the initializer that I am using wrong or is there a way to avoid the error?

Update: Problem solved, see comments!

Upvotes: 0

Views: 1534

Answers (2)

TimCo
TimCo

Reputation: 41

Also, earlier release (and some Videos) of "Xcode 11 Beta" show use of "NavigationButton" but it is no longer supported, has been replaced with "NavigationLink".

Upvotes: 2

Hussain Shabbir
Hussain Shabbir

Reputation: 15025

Try below code:-

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationButton(destination: DetailView()) {
                Text("Click")
                }.navigationBarTitle(Text("Navigation"))
        }
    }
}
struct DetailView: View {
    var body: some View {
        Text("Detail")
    }
}
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 1

Related Questions