[英]How do I use UI button to call a new controller in swift?
I've got an instance of a button and I also have two textfields for usernames and passwords. 我有一个按钮的实例,并且我还有两个用于用户名和密码的文本字段。 it is a test app therefor I have hard coded usernames and password and passed strings into it as such
这是一个测试应用程序,我具有硬编码的用户名和密码,并像这样将字符串传递给它
@IBOutlet weak var usernameTextfield: UITextField!
@IBOutlet weak var passwordTextfield: UITextField!
let username = "itishim"
let password = "password"
I just created a new view controller (viewcontroller2), drag it to story board and controlled dragged the main view controller to the new view controller. 我刚刚创建了一个新的视图控制器(viewcontroller2),将其拖到故事板上,并控制将主视图控制器拖到新的视图控制器中。 My goal is to use the button to segue to the new view controller if the password I hardcoded is true.
我的目标是,如果我硬编码的密码为true,则可以使用该按钮选择到新的视图控制器。
@IBAction func button(sender: AnyObject) {
if usernameTextfield.text == username && passwordTextfield.text == password {
self.performSegueWithIdentifier("nextView", sender: self)
} else {
print("nope")
I keep getting "[UIViewController button:]: unrecognized selector sent to instance 0x7f8731515400" even though I have the right username and password. 即使我拥有正确的用户名和密码,我仍然收到“ [UIViewController按钮:]:无法识别的选择器发送到实例0x7f8731515400”的消息。 I am new to swift and would appreciate any help I get.
我是新手,也很高兴能得到我的帮助。 Thanks!
谢谢!
I checked your zip file. 我检查了您的zip文件。 It crashes even though there is no segue.
即使没有segue,它也会崩溃。 I propose you built a new project.
我建议您建立一个新项目。 And do the following.
并执行以下操作。
And then it the button function 然后是按钮功能
@IBAction func ButtonFuntion(_ sender: AnyObject) {
if usernameTextfield.text == username && passwordTextField.text == password{
performSegue(withIdentifier: "nextView", sender: nil)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.