Both methods allow me to present a new view controller (one by calling presentviewcontroller), so I don't understand the difference between the two and when I should use them.
Use segues to define the flow of your app's interface. A segue defines a transition between two view controllers in your app's storyboard file. The starting point of a segue is the button, table row, or gesture recognizer that initiates the segue. The end point of a segue is the view controller you want to display.
Segues are visual connectors between view controllers in your storyboards, shown as lines between the two controllers. They allow you to present one view controller from another, optionally using adaptive presentation so iPads behave one way while iPhones behave another.
A relationship segue is a connection between a navigation controller and its root view controller. Or the connection between a tab bar controller and its view controllers.
In UIKit, a segue is an object that defines a transition between two view controllers in a storyboard file. An action — such as tapping a button, performing a gesture or tapping on a table cell row — triggers the segue. The endpoint of the segue is the view controller you want to display.
They both reference storyboard related identifiers. The main difference is one (performSegueWithIdentifer
) instantiates an object based on a segue's end (where the segue points to), while the other (instantiateViewControllerWithIdentifier
) instantiates a unique VC based on the VC's identifier (not the segue).
You can have multiple segue's with the same identifier in different places in the storyboard, while VC's in a storyboard cannot have the same identifier.
performSegueWithIdentifer and instantiateViewControllerWithIdentifier both are used to move from one viewController to another viewController. But there is so much differences....
The identifier of the 1st case defines a segue like push, modal, custom etc which are used to perform a specific type of transition from one VC to another VC. eg.
self.performSegueWithIdentifier("push", sender: self);`
where "push" is an identifier of a push segue.
The identifier of the 2nd case defines a VC like myViewController, myTableViewController, myNavigationController etc. 2nd function is used to go to the specific VC ( with identifier.) from a VC in the storyBoard. eg.
var vc = mainStoryboard.instantiateViewControllerWithIdentifier("GameView") as GameViewController;
self.presentViewController(VC, animated: true, completion: nil) ;
where "GameView" is the identifier of GameViewController. Here a instance of GameViewController is created and then the function presentViewController is called to go to the instantiated vc.
For the 1st case with the help of segue identifier u can pass one are more values of variables to the next VC. eg.
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if (segue.identifier == "push")
{
let game = segue.destinationViewController as GameViewController
game.value = self.myvalue // *value* is an Int variable of GameViewController class and *myvalue* is an Int variable of recent VC class.
}
}
This funcion is also called when self.performSegueWithIdentifier("push", sender: self); is called to pass the value to GameViewController.
But in 2nd case it possible directly like,
var vc = mainStoryboard.instantiateViewControllerWithIdentifier("GameView") as GameViewController;
vc.value = self.myvalue;
self.presentViewController(VC, animated: true, completion: nil) ;
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