我想用我做的按钮导航到另一个视图。我是为另一个视图做的,但是当将它应用到下一个视图时,它显示了错误“表达式的类型不明确,没有更多上下文。”我从其他人那里读到,SwiftUI中的错误有时不会出现在正确的行上,可能会发生在其他地方。
下面是我编写的用于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
    }
}

下面是我用同样的方法编写的代码,但它给了我一个错误。我没有导航链接中文本的任何内容,因为我正在使用按钮,所以我将其保留为空。
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
    }
}

最佳答案

NavigationLink的第一行中使用NavigationView而不是var body
所以这个:

var body: some View {
    NavigationLink{
    ,,,
    }//nav
}

应该是:
var body: some View {
    NavigationView{
    ,,,
    }//nav
}

struct signUp就像一个导航触发器,而NavigationLink负责处理视觉、动画和其他有关导航的内容。
请注意,由于SwiftUI非常年轻,Xcode无法准确地检测出问题所在,有时会显示错误或不清楚的消息。但苹果一直在努力改进它,并一次又一次地发布。

关于ios - 带有SwiftUI的NavigationLink:表达式类型不明确,没有更多上下文,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58825323/

10-11 16:21