使用SwiftUI的NavigationLink:没有更多上下文,表达式的类型不明确

ijnw1ujt  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(219)

我正在尝试使用我制作的按钮导航到另一个视图。我为另一个视图做了这个,但是当应用到下一个视图时,它显示错误“Type of expression is ambiguous without more context.”我从其他人那里阅读到,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
    }
}

下面是我写的代码,以同样的方式应用它,但是给了我那个错误。我没有任何内容用于NavigationLink中的文本,因为我正在使用按钮,所以我将其留空。

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
    }
}
oaxa6hgo

oaxa6hgo1#

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

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

应为:

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

NavigationLink就像是导航的触发器,而NavigationView负责处理视觉效果、动画和其他关于导航的东西。
请注意,由于SwiftUI非常年轻,Xcode无法准确检测出问题所在,有时会显示错误或不清楚的消息。但苹果公司一直在努力,并不断改进它的版本。

相关问题