Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move to next view controller programmatically

Tags:

swift

uibutton

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")
    }
}
like image 701
Limyandi Vico Trico Avatar asked Sep 06 '16 02:09

Limyandi Vico Trico


People also ask

How do I move between view controllers?

Ctrl+Drag from the “View Controller” button, to somewhere in the second View Controller(HomeViewController). It can be anywhere in the main box of the second view controller. When you release, it will show you a box like the one below. in this you don't need any code to switch, it will switch on click of a button.


2 Answers

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.

like image 73
Aks Avatar answered Nov 09 '22 13:11

Aks


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!

like image 44
David Park Avatar answered Nov 09 '22 12:11

David Park