What is the difference between using the instantiateViewControllerWithIdentifier and performseguueithidentifier method?

Both methods allow me to introduce a new view controller (one of which is called presentviewcontroller), so I do not understand the difference between them and when I should use them.

+7
oop ios swift
source share
2 answers

They both refer to identifiers associated with the storyboard. The main difference is that one ( performSegueWithIdentifer ) creates an object based on the end of the segue (where the segment indicates), while the other ( instantiateViewControllerWithIdentifier ) creates a unique VC based on the VC identifier (not for the segment).

You can have multiple segue with the same identifier in different places of the storyboard, while the VC in the storyboard cannot have the same identifier.

+7
source share

performSegueWithIdentifer and instantiateViewControllerWithIdentifier are both used to move from one viewController to another viewController. But there are so many differences ...

  • The identifier of the 1st case defines the segue of type push, modal, custom, etc., which are used to perform a certain type of transition from one VC to another VC. eg.

     self.performSegueWithIdentifier("push", sender: self);` 

    where "push" is the identifier of the push segment.

    Case ID identifies the VC, e.g. myViewController, myTableViewController, myNavigationController, etc. The second function is used to go to a specific VC (with identifier.) From VC to storyBoard. eg.

     var vc = mainStoryboard.instantiateViewControllerWithIdentifier("GameView") as GameViewController; self.presentViewController(VC, animated: true, completion: nil) ; 

    where "GameView" is the identifier of the GameViewController. This creates an instance of the GameViewController and then calls the presentViewController function to go to the vc instance.

  • For the 1st case, with the help of the identifier segue u one can pass, this is more than the values โ€‹โ€‹of the variables for 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 function is also called when self.performSegueWithIdentifier ("push", sender: self); called to pass the value to the GameViewController.

    But in the second case, it is possible directly, like,

     var vc = mainStoryboard.instantiateViewControllerWithIdentifier("GameView") as GameViewController; vc.value = self.myvalue; self.presentViewController(VC, animated: true, completion: nil) ; 
+6
source share

All Articles