UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I navigate to new View using an if statement

Forums > watchOS

I am getting an error statement "Cannot infer contextual base in reference to member 'NavigationLink'". How do I fix it so that I can get to a view from there without making any extra buttons?

import SwiftUI

var logged = false

struct Sign_In: View {
    @State private var username = ""
    @State private var password = ""
    @State var tag : Int? = nil

    var body: some View {
        Form {
            TextField("Username", text: $username)
            SecureField("Password", text: $password)
        }

        .onSubmit {
            guard username.isEmpty == false && password.isEmpty == false else { return }
            let logged = true
            print(logged)
            if logged == true {
                self.tag = 1
                print("Check")
            }
            NavigationStack{
                .NavigationLink(destination: Schedule(), tag: 1, selection: $tag) { /*this is where i'm getting my error*/
                    EmptyView()
                }.disabled(true)
            }

        }
        .submitLabel(.next)

    }
}

4      

FIxed, sorry.

4      

From my understanding NavigationStack should be the root in your View. For me it's not clear what you're trying to accomplish with EmptyView, either.

4      

You probably can simplify this guard statement for clearer meaning:

guard username.isEmpty == false && password.isEmpty == false else { return }

Maybe explore:

guard !username.isEmpty && !password.isEmpty else { return }

While the change may make the code more consise, what happens when you want to add more validation logic? This adds more code, but maybe it clarifies your logic?

// Add any condition that makes the form incomplete.
var incompleteForm: Bool {
    if username.isEmpty { return true }
    if password.isEmpty { return true }
    // Easily add more validation code ------------
    // if zipcode.isEmpty { return true }
    // if emailAddress.isEmpty { return true }
    // if emailAddress.isMalformed { return true }

    return false
}

    [... snip ...]
    guard !incompleteForm else { return } // <-- Check with computed variable

4      

From your profile, I see that you joined yesterday, and jumped into the forums with a NavigationStack problem.

@Hatsushira, a frequest poster, notes that your NavigationStack is declared incorrectly.

Also, the NavigationStack recipe typically calls for SwiftUI views to declare clickable items within the NavigationStack that will be NavigationLinks. Links are views with a specific type such as LogInView, BeefPlatterView, LandmarkView, even Text()counts as a clickable Link.

Then when a NavigationLink is tapped, the final part of the recipe is the NavigationDestination. If you tapped a BeefPlatterView, you might be taken to a BeefMenuDetailView. If you tapped a LandmarkView, you'll instead be shown a LandmarkDetailView.

From your code snip above, it doesn't look like you're following a typical NavigationStack / NavigationLink / NavigationDestination recipe.

Instead of trying to teach this concept in a forum response, you'd learn much more following @twoStraw's step-by-step videos and following his excellent tutorials.

Introduction to Navigation

Aren't you lucky?! @twoStraws updated his step-by-step tutorials just last month! (Dec 2022). Grab a cuppa, have a nice read, sling some simple code, enjoy the thoroughness of his videos, smile at Luna and Aria, and come back here when you get stuck!

Quoting from his first page:

❝[NavigationStacks] aren’t necessarily hard, they just take more planning to get right.❞

See -> NavigationStacks

Keep Coding!

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.