How to find out which segue was used with Swift?

I have a main viewController and detailViewController. There are 2 buttons in the detailViewController. Both buttons go to the main controller, but I want to configure the main viewController based on which segue was used. What is the best way to check which segue was used to access the viewController so that you can configure the main viewController depending on this? - if segue1 leads to the main view manager, then I want label1 hidden. if segue2 leads to the main viewController, then I want label2 to be hidden.

+5
source share
3 answers

In the Main View Controller, create a variable, something like

var vcOne : Bool = true 

Now in DetailsViewController

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if segue.identifier == "segue_one" { let mainVC : MainViewController = segue.destinationViewController as! MainViewController secondVC.vcOne = true } else if segue.identifier == "segue_two" { let mainVC : MainViewController = segue.destinationViewController as! MainViewController secondVC.vcOne = false } } 

Now in MainView Controller

  override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) //Now check here for which segue if(vcOne) { // implement for button one click } else { // implement for button two click } } 

Hope this helps you

+7
source

I would do something like to check which segue was used. You have to set the identifier in segue in the storyboard though!

  func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "yourIdentifier" { let yourVC = segue.destinationViewController as? yourViewController //do magic with your destination } } 
+1
source

There is an option to set an identifier for segue. This must be a unique identifier. So that you can identify which section is activated. Example:

 func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "Identifier1" { let firstVC = segue.destinationViewController as? FirstViewController } else if segue.identifier == "Identifier2" { let secondVC = segue.destinationViewController as? SecondViewController } } 
0
source

Source: https://habr.com/ru/post/1245961/


All Articles