shjnlee
shjnlee

Reputation: 243

Swift - how to transfer from one view controller to another?

Back to my first swift app, I'm doing the sign in/sign up part, and basically the design with the view controllers so far are:

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

I got to the part where in the sign up view controllers, if the user misses one of the required field blanks and they click the button "submit", they will be prompted and back to the current view controller (sign up) to fill the missing fields. Now I want to set that when all the fields are filled, what line of code can I use so when they click the button "submit" it will head back to the welcome screen, so they can sign in afterwards?

Similarly, I got all the the part to check if the user enters correct user name and password, so when they entered the wrong info, they will be prompted and hack to the current view controller (sign in), and how can I do so if they entered the correct user name - password, it will head to the main program view controller (the 4th one)

If what I said above is confusing, I'll post my current controllers and the related code in here. Anyone has any idea? Again, this is my first program so it'd mean very much if I can get some helps from you. Thank you for reading.

Upvotes: 0

Views: 6066

Answers (3)

wondertalik
wondertalik

Reputation: 718

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBar = storyBoard.instantiateViewController(withIdentifier: "MainTabBar") as! UITabBarController
self.navigationController?.pushViewController(mainTabBar, animated: true)

works for me. thanks! update for the swift 3.1 and Xcode 8.3.2

Upvotes: 0

Akash Raghani
Akash Raghani

Reputation: 557

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)

Upvotes: 1

Lukas
Lukas

Reputation: 3433

An example on how to do it:

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

Upvotes: 2

Related Questions