首页 文章

Swift - 如何从一个视图控制器转移到另一个视图控制器?

提问于
浏览
0

回到我的第一个swift应用程序,我正在进行登录/注册部分,基本上到目前为止,视图控制器的设计是:

Welcome screen (sign in/sign up buttons) - Sign in - Sign up - Main Program

我到达了注册视图控制器中的部分,如果用户错过了一个必需的字段空白并且他们点击了“提交”按钮,他们将被提示并返回当前视图控制器(注册)以填充缺少字段 . 现在我想设置当所有字段都填满时,我可以使用哪一行代码,所以当他们点击“提交”按钮时,它将返回到欢迎屏幕,以便他们可以在之后登录?

同样,我得到了所有部分来检查用户是否输入了正确的用户名和密码,因此当他们输入错误的信息时,他们会被提示并入侵当前的视图控制器(登录),我该怎么办呢如果他们输入了正确的用户名 - 密码,它将转到主程序视图控制器(第4个)

如果我上面说的很混乱,我会在这里发布我当前的控制器和相关代码 . 任何人有任何想法?再次,这是我的第一个程序,所以如果我能从你那里得到一些帮助,那就非常有意义 . 谢谢你的阅读 .

3 回答

  • 0

    关于如何做到这一点的一个例子:

    import UIKit
    
    class WelcomeVC: UIViewController {
    
        //make it IBAction or call it from IBAction
        func singInButtonPressed(){
            performSegueWithIdentifier("sign in", sender: self)
        }
        //make it IBAction or call it from IBAction
        func singUPButtonPressed(){
            performSegueWithIdentifier("sign up", sender: self)
        }
    }
    
    class SignUPVC: UIViewController {
        @IBOutlet var userNameField:UITextField!
        @IBOutlet var passwordField:UITextField!
    
        //make it IBAction or call it from IBAction
        func submit(){
            if checkValidity() {
                presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
            } 
        }
    
        func checkValidity() ->Bool{
            if !userNameField.text!.isEmpty{
                if !passwordField.text!.isEmpty {
                    return true
                } else {
                    //do something to inform passwordfield is missing
                }
            } else{
                //username field is missing
            }
            return false
        }
    
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if segue.identifier == "sign in" {
                if let sINvc = segue.destinationViewController.contentViewController as? SignInVC{
                    sINvc.username = userNameField.text
                    sINvc.password = passwordField.text
    
                    //and pass more of the other info you gathered in this vc
                }
            }
        }
    
    }
    class SignInVC: UIViewController {
    
        @IBOutlet var userNameField:UITextField!{
            didSet { 
                if username != nil { userNameField.text = username }
            }
        }
        @IBOutlet var passwordField:UITextField! {
            didSet { 
                if password != nil { passwordField.text = password }
            }
        }
    
        var password:String?
        var username:String?
    
        //make it IBAction or call it from IBAction
        func signInPressed(){
            if !userNameField.text!.isEmpty && !passwordField.text!.isEmpty {
                performSegueWithIdentifier("Main page", sender: self)
            }
        }
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if segue.identifier == "Main page"{
                //your preparation 
            }
        }
    }
    
    //this extension is helpful to avoid typing this everytime there is nav vc in your way of segueing
    extension UIViewController{
        var contentViewController: UIViewController {
            if self is UINavigationController{
                if let cvc = (self as! UINavigationController).visibleViewController { return cvc }
            }
            return self 
        }
    }
    
  • 2
    simple way to transfer one view to another in swift is this given below:-
    
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
        //in "Main" your  storyboard name
    
    
        let secondViewController = storyboard.instantiateViewControllerWithIdentifier("LoginPage") as! UIViewController
    
        //in place of Login Page your storyboard identifier name 
    
       navigationController?.pushViewController(secondViewController, animated: true)
    
  • 1
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let mainTabBar = storyBoard.instantiateViewController(withIdentifier: "MainTabBar") as! UITabBarController
    self.navigationController?.pushViewController(mainTabBar, animated: true)
    

    适合我 . 谢谢!更新swift 3.1和Xcode 8.3.2

相关问题