6

I wonder how to create a function that move to the login page after click login button, with code/programmatic way.

There is my code:

final class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let loginButton = UIButton(frame: CGRectMake(20, 640, 175, 75))
        loginButton.setTitle("Login", forState: UIControlState.Normal)
        loginButton.backgroundColor = UIColor.init(red: 255, green: 215, blue: 0, alpha: 0.8)
        loginButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        loginButton.titleLabel!.font = UIFont(name: "StarJediSpecialEdition", size: 30)
        loginButton.addTarget(self,
                              action: #selector(ViewController.tapActionButton),
                              forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(loginButton)
    }

    func tapActionButton(sender:UIButton!) {
        print("Button is working")
    }
}
Zaporozhchenko Oleksandr
  • 4,660
  • 3
  • 26
  • 48
  • 2
    you are probably looking for performseguewithidentifier http://stackoverflow.com/questions/32292600/swift-performseguewithidentifier-not-working – Leo Dabus Sep 06 '16 at 02:35
  • If he Is a beginner, he is preferably looking for self.presentViewController, because segues required extra information and learning about storyboards and how all that works with segues. – Zaporozhchenko Oleksandr Feb 27 '20 at 04:55

2 Answers2

14

There are multiple ways how you create your login page UIViewController:

  1. From xib, for example, you can inherit your controller from this class:
class XibLoadedVC: UIViewController {
        required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
        required init() {
            print("init \(type(of: self))")
            let bundle = Bundle(for: type(of: self))
            super.init(nibName: String(describing: type(of: self)), bundle: bundle)
        }
    }

or just

let controller = LoginPageViewController(nibName: "LoginPageViewController", bundle: nil)
  1. From storyboard:
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let loginPage = storyboard.instantiateViewControllerWithIdentifier("LoginPageViewController") as! LoginPageViewController

There are multiple ways to show controller depending on how you are create it:

  1. You can just present controller with:
func tapActionButton(sender: UIButton!) {
    let loginPage = LoginPageViewController()
    self.present(loginPage, animated: true)
}
  1. Programmatically push using navigationController:
navigationController?.pushViewController(loginPage, animated: true)

Requires UINavigationController to work. As you can’t be sure, is your controller inside navigation controller stack or not, there is optional navigationController? to not crush the app :)

  1. Storyboard and Segues:

Create segue between your ViewController and LoginPageViewController. Give your segue an identifier and also presentation style. For this case it will be Show Example

Now in your ViewController override below method

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if segue.identifier == "YourSegueIdentifier"
       let loginPage = segue.destinationViewController as! LoginPageViewController
   }
}

On loginButton tap action:

func tapActionButton(sender: UIButton!) {
    performSegueWithIdentifier("YourSegueIdentifier", sender: nil)
}

And you are done.

P.S. Also, there is modern SwiftUI way to do all that, you can look into tutorials and official documentation.

Zaporozhchenko Oleksandr
  • 4,660
  • 3
  • 26
  • 48
Aks
  • 1,567
  • 13
  • 23
  • your answer is actually very detailed, but I am a beginner, so would you tell me please where should i put the code as there is always an error telling me that the name of the view controller I want to present next is undeclared. Thankyou! – Limyandi Vico Trico Sep 06 '16 at 23:59
  • The code should be put in LoginViewController. And from the error I think you haven't declared your next viewController. – Aks Sep 09 '16 at 07:59
  • When should i go with any option mentioned above? – Amjad Husseini Aug 07 '17 at 09:12
  • Both ways are totally valid and give you the same result, It's developers choice which one to follow. – Aks Aug 09 '17 at 11:40
5

You need to create an instance of the next view controller and then either present or push that view controller

To present modally:

self.presentViewController(nextVC, animated: true, completion: nil)

or to push:

self.navigationController?.pushViewController(nextVC, animated: true)

all that stuff with segues apply to storyboards

Cheers!

Zaporozhchenko Oleksandr
  • 4,660
  • 3
  • 26
  • 48
David Park
  • 325
  • 2
  • 11