简体   繁体   中英

transitioning from one view controller to another using a button

So, I created a login page with basic logic. If username and/or password are empty, it displays an error message. If they're full, then it transitions to the next page (home). I created a second ViewController on the storyboard, and created a SecondViewController". I then defined the respective ViewController class to "SecondViewController".

On the first ViewController.swift file, i used this code:

func transition(Sender:UIButton!)
{
    let secondViewController: SecondViewController = SecondViewController()
    self.presentViewController(secondViewController, animated:true, completion:nil)

}

When I tested it out however, pressing the login button (when both the username and password are filled) transfers me to a black screen instead of the second View Controller. Any ideas on what to do?

You can't initialize a view controller with just the default init method.

You should probably create a view controller scene in your storyboard, add a unique identifier on it, instantiate it using instantiateViewControllerWithIdentifier: , and then display it like you are doing.

You'll want to make a segue between the view controllers in your Storyboard, and call self.performSegueWithIdentifier() .

Or you can give your `UIViewController an identifier and present it programmatically too:

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let secondViewController storyboard.instantiateViewControllerWithIdentifier("") as? SecondViewController
if let secondViewController = secondViewController {
    self.presentViewController(secondViewController, animated:true, completion:nil)
}

If you want the view to contain things added to it on the storyboard, you could give it a Storyboard ID (a couple of boxes below where you set the class of the view controller in the storyboard).

If you give it for example the ID "secondVC" you can then create it using the following:

let secondViewController = storyboard?.instantiateViewControllerWithIdentifier("secondVC") as? SecondViewController

There are basically three ways to do this

1)Creating segue in storyboard,though it will only work if you have single segue from that item

2)using prepareforSegue and performSegue Methods in your presenting view controller override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "YourIdentifier" {
  let controller = segue.destinationViewController as!secondViewController
}

}

Then use this in your IBACtion

performSegueWithIdentifier("YourIdentifier", sender:sender)

3)(Recommended One)

let controller = self.storyboard?.instantiateViewControllerWithIdentifier("ContactsVC")! as! ContactsVC 

self.presentViewController(controller, animated:true, completion:nil

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM