如果单击按钮并且满足其他条件,如何切换到第二场景?

问题描述 投票:-1回答: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")))
         {


         }
         else
         {
             print("ERROR")
         }

     }


 }

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

swift xcode switch-statement scene
1个回答
0
投票
  • 使用storyBoard标识符

下面是更新的代码

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")
     }

 }

}

© www.soinside.com 2019 - 2024. All rights reserved.