另一个快速的初学者在这里。
基本上要打造一个受密码保护的视图控制器。
至今 -
创建的故事板 -在viewcontroller上 - 创建硬编码登录 -如果…打印到控制台
您可以使用segue或instantiateViewController。但在这个例子中,我将使用instantiateViewController(Images)。 (但评论了如何使用segue)
但首先,让我们来看看你提供的代码。
@IBAction func loginbtn(_ sender: Any) { if untext.text == username && pwtext.text == password { print("login succeeded") //1. using instantiateViewController if let storyboard = storyboard { //Check my image below how to set Identifier etc. // withIdentifier = Storyboard ID & "ViewController" = Class let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController self.present(vc, animated: false, completion: nil) } //2. Use segue (I'll wrap this with a comment incase you copy) //self.performSegue(withIdentifier: "SegueID", sender: self) } else { //Setting up an "AlertController" let alert = UIAlertController(title: "Login failed", message: "Wrong username / password", preferredStyle: UIAlertController.Style.alert) //Adding a button to close the alert with title "Try again" alert.addAction(UIAlertAction(title: "Try again", style: UIAlertAction.Style.default, handler: nil)) //Presentating the Alert self.present(alert, animated: true, completion: nil) } }
单击ViewController上的黄点(在ViewController上,您希望登录页面带您)
点击像我这样的图标。 (哪个是蓝色)并设置一个Class + Storyboard ID。
注意!如果你想使用segue,请确保ViewController(Login)和ViewController1之间有连接
假设你使用segues进行导航,你可以设置一个“通用”segue(从你的控制器拖动而不是其中的任何控件)并为它分配一个ID(Storyboard中segue的属性检查器中的标识符)。之后,您可以使用您的代码有条件地从父控制器类调用segue:
if passwordCorrect { performSegue(withIdentifier: "SegueID", sender: nil) }