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")
}
}
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.
There are multiple ways how you create your login page UIViewController:
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)
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:
func tapActionButton(sender: UIButton!) {
let loginPage = LoginPageViewController()
self.present(loginPage, animated: true)
}
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 :)
Create segue between your ViewController and LoginPageViewController.
Give your segue an identifier and also presentation style. For this case it will be Show
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With