I'm trying to use a button I made to navigate to another view. I done it for the other view, but when applying it to the next one it shows the error "Type of expression is ambiguous without more context." I've been reading from other people that errors in SwiftUI don't appear on the right line sometimes and can be happening somewhere else.
Here's the code I wrote that worked with NavigationLink
struct ContentView: View {
@State private var isActive: Bool = false
@State private var username: String = ""
@State private var email: String = ""
@State private var password: String = ""
var body: some View {
NavigationView{
VStack {
NavigationLink(destination: signUp(), isActive: self.$isActive) {
Text("")
}
Image("versLogo")
.resizable()
.frame(width: 400, height: 400)
TextField("Username", text: $username)
TextField("Email", text: $email)
TextField("Password", text: $password)
Button(action: {}) {
Text("Login")
}
Spacer()
//button for moving to next view
Button(action: {
self.isActive = true
}) {
Text("Don't have an account?")
}
}//VStack
}//nav
}
}
Now here's the code I wrote applying it the same way, but is giving me that error. I don't have anything for the text in the NavigationLink because I'm using the button so I left it empty.
struct signUp: View {
@State var isShowingImagePicker = false
//for the next view
@State private var isActive: Bool = false
@State private var username: String = ""
@State private var email: String = ""
@State private var password: String = ""
@State private var confirm: String = ""
var body: some View {
NavigationLink{
VStack {
NavigationLink(destination: DOB_finalSignUp(), isActive: self.$isActive) {
Text("") //ERROR is on this line
}
Image(uiImage: UIImage())
.frame(width: 200, height: 200)
.border(Color.black, width: 1)
Button(action: {
self.isShowingImagePicker.toggle()
}, label: {
Text("Select Image")
})
TextField("Username", text: $username)
TextField("Email", text: $email)
TextField("Password", text: $password)
TextField("Confirm Password", text: $confirm)
//button for moving to next view
Button(action: {
self.isActive = true
}) {
Text("Continue")
}
}//VStack
}//nav
}
}
You used NavigationLink
instead of NavigationView
in the very first line of var body
inside struct signUp
.
So this:
var body: some View {
NavigationLink{
,,,
}//nav
}
should be:
var body: some View {
NavigationView{
,,,
}//nav
}
NavigationLink
is like a trigger to navigate, while NavigationView
is responsible to handle visuals, animations and other stuff about navigating.
And note that since SwiftUI is very young, Xcode is not able to detect what is the issue exactly and sometimes shows wrong or unclear messages. But apple is constantly working on it and improving it release by release.