简体   繁体   English

如何使用UI按钮快速调用新控制器?

[英]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. 并执行以下操作。

  1. Drag a viewController and set it with a new SecondViewController.swift 拖动viewController并使用新的SecondViewController.swift进行设置
  2. Go to storyBoard and from the firstViewController make a segue to the secondviewController by draging from the first (yellow ) button at the top the secondViewCOntroller and click show. 转到storyBoard,然后从firstViewController通过从第二个ViewCOntroller顶部的第一个(黄色)按钮拖动到第二个ViewController,然后单击显示。
  3. Add your textFields and button 添加您的textFields和按钮
  4. Add button action in viewCOntroller. 在viewCOntroller中添加按钮操作。
  5. Click the segue in storyboard and give it name "nextView" 单击情节提要中的segue,并将其命名为“ nextView”

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.

相关问题 使用Swift,如何从另一个视图控制器中的按钮调用在一个视图控制器中编码的函数? - Using Swift, how do I call a function coded in one view controller from a button in another view controller? 如何使用swift从视图控制器调用SetRootViewController? - how do i call SetRootViewController from a view controller using swift? 如何通过快速点击按钮创建新视图(或子视图)? - How do I create a new View (or subView) with the tap of a button in swift? 如何在按钮操作调用中强制重绘 swift ui 视图 - How to forcefully redraw a swift ui view within a button action call 如何通过单击按钮功能快速调用标签栏视图控制器 - how to call tab bar view controller by click button function in swift 如何将按钮连接到新的视图控制器iOS Swift? - How to connect a button to a new view controller iOS Swift? 如何在 Swift 中编写 UI 测试来测试如果点击按钮,标签是否可见? - How do I write a UI test in Swift to test whether a label becomes visible if a button is tapped on? 如何从另一个视图控制器重命名按钮标题集(Swift 3) - How do I rename a button title set from another view controller (Swift 3) iOS如何更改现有的UI以使用新的导航控制器 - IOS How to alter an existing UI to use a new navigation controller 如何推送不在导航控制器中的视图控制器? 迅速 - How do I push a View Controller that's not in a navigation controller? Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM