【问题标题】:Whenever I click on button the view won't open and give me this warnings, what am I doing wrong in this code每当我单击按钮时,视图都不会打开并给我这个警告,我在这段代码中做错了什么
【发布时间】:2022-01-16 04:19:11
【问题描述】:

下面的代码在按钮部分给了我警告,如果我运行视图将不会打开。

如果我尝试使用 NavigationLink,最后会出现一个箭头,我不想以任何方式使用导航。

我尝试了将按钮视图放入 NavigationLink 链接,但是当我点击它时,整个表单具有点击事件效果,就像 ram 点击整个表单一样。

import SwiftUI
//login view
    struct login: View {
        @State private var usern:String=""
        @State var navigated = false
        @State private var pass:String=""
        @State private var secured:Bool=true
        var body: some View {
                NavigationView{
//textfield and password field
                    Form{
                        VStack{
                        HStack{
                    Text("UserName:")
                    TextField("", text:  $usern)
                        }
                            HStack{
                            Text("Password:")
                                if secured{
                                    SecureField("",text:$pass)
                                }
                                else{
                                    TextField("",text: $pass)
                                }
                                Button(action: {self.secured.toggle()}){
//password seen or unseen
                                    if secured{
                                        Image(systemName:"eye.slash")
                                    }
                                    else{
                                        Image(systemName:"eye")
                                    }
                                }.buttonStyle(BorderlessButtonStyle())
                            }
                            Button("Login",action: {signup()}).buttonStyle(BorderlessButtonStyle())
 //button view not working
                            Button("Not Yet signed? Create new account",action: {signup()}).buttonStyle(BorderlessButtonStyle()) 
//button view not working
                    }
                }
        }
        
    }
}
    
struct login_Previews: PreviewProvider {
    static var previews: some View {
        login()
    }
}

//SIGNUP VIEW 
import SwiftUI

struct signup: View {
    var body: some View {
        Text("Hello, World!")
    }
}

struct signup_Previews: PreviewProvider {
    static var previews: some View {
        signup()
    }
}

【问题讨论】:

  • 警告说什么?
  • 欢迎来到 Stack Overflow!请拨打tour 并查看:How do I ask a good question?。您实际上还没有说过要如何显示您的注册屏幕。您希望它在工作表中还是在新视图中?此外,警告是因为您不能只将 View 放入 Button 的操作部分。

标签: xcode button swiftui


【解决方案1】:

按钮的目的是调用一个动作,切换一些东西。 要解决您的问题,您必须:

在登录视图中添加State变量

struct login: View {
    @State private var usern:String=""
    @State var navigated = false
    @State private var pass:String=""
    @State private var secured:Bool=true
    @State var showSignUp:Bool = false -- this one
}

更改按钮定义 FROM

Button("Not Yet signed? Create new account",action: {signup()}).buttonStyle(BorderlessButtonStyle())

Button("Not Yet signed? Create new account",action: {showSignUp.toggle()}).buttonStyle(BorderlessButtonStyle())
                    .sheet(isPresented: $showSignUp) {
                        signup()
                    }

请注意,在本例中,我使用 .sheet(isPresented) 来调用视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2015-02-25
    • 2019-06-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多