1

I have a custom NavigationLink in SwiftUI. Now I'm trying to add isActive to my customNavLink but I'm facing with Missing argument for parameter 'isActive' in call across all project. I want to make this isActive optional to use where I need it.

Do you know how could I resolve this issue?

This is My CustomNavLink

struct CusNavLink<Label: View, Destination: View>: View {

    let destination: Destination
    let label : Label
    let isActive: Binding<Bool>

    init(destination: Destination, isActive: Binding<Bool>, @ViewBuilder label: () -> Label) {
        self.destination = destination
        self.label = label()
        self.isActive = isActive
    }

    var body: some View {
        NavigationLink(
            destination: CusNavContainer{
                destination
            }
                .navigationBarHidden(true),
            isActive: isActive,
            label:{
                label
            })
    }
}

1 Answer 1

1

If you want isActive to be Optional, you'd have to declare it as such in the initializer and properties. Then, you'd conditionally display a different NavigationLink initializer depending on if you have an isActive Binding to pass it:

struct CusNavLink<Label: View, Destination: View>: View {

    let destination: Destination
    let label : Label
    let isActive: Binding<Bool>?

    init(destination: Destination, isActive: Binding<Bool>? = nil, @ViewBuilder label: () -> Label) {
        self.destination = destination
        self.label = label()
        self.isActive = isActive
    }

    var body: some View {
        if let isActive = isActive {
            NavigationLink(
                destination: CusNavContainer {
                    destination
                }
                .navigationBarHidden(true),
                isActive: isActive,
                label:{
                    label
                })
        } else {
            NavigationLink(
                destination: CusNavContainer {
                    destination
                }
                .navigationBarHidden(true),
                label:{
                    label
                })
        }
    }
}
Sign up to request clarification or add additional context in comments.

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.