【问题标题】:How to switch to second scene if button clicked and other conditions met?如果单击按钮并满足其他条件,如何切换到第二个场景?
【发布时间】:2020-01-11 00:29:18
【问题描述】:

我是 swift 的新手,当输入正确的电子邮件和密码并在 swift 中按下登录按钮时,我正尝试切换到第二个场景。

到目前为止,这是我的代码:

 class FirstScreen: UIViewController {

     @IBOutlet weak var headerLabel: UILabel!
     override func viewDidLoad() {
         super.viewDidLoad()

     }


     @IBOutlet weak var emailLabel: UITextField!
     @IBOutlet weak var passwordLabel: UITextField!



     @IBAction func loginButton(_ sender: UIButton)
     {
         let emailLabel1 = emailLabel.text!
         let passwordLabel1 = passwordLabel.text!
         if((emailLabel1.contains("email")) && (passwordLabel1.contains("password")))
         {


         }
         else
         {
             print("ERROR")
         }

     }


 }

我将如何以编程方式切换到 if 语句中的第二个场景?我正在使用 xCode 11 和 swift 5.1。任何帮助都会非常感谢

【问题讨论】:

标签: swift xcode switch-statement scene


【解决方案1】:
  • 使用故事板标识符

更新代码如下

class FirstScreen: UIViewController {

 @IBOutlet weak var headerLabel: UILabel!
 override func viewDidLoad() {
     super.viewDidLoad()

 }


 @IBOutlet weak var emailLabel: UITextField!
 @IBOutlet weak var passwordLabel: UITextField!



 @IBAction func loginButton(_ sender: UIButton)
 {
     let emailLabel1 = emailLabel.text!
     let passwordLabel1 = passwordLabel.text!
     if((emailLabel1.contains("email")) && (passwordLabel1.contains("password")))
     {
         let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
         let secondView =  storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
         if #available(iOS 13.0, *) {
             self.navigationController!.pushViewController(secondView, animated: false)
         }else{
             self.present(secondView, animated: true, completion: nil)
         }


     }
     else
     {
         print("ERROR")
     }

 }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 2022-11-02
    • 2018-08-24
    相关资源
    最近更新 更多