I got two segue's which lead to the same viewController
. There are 2 buttons which are connected to the same viewController
using 2 segues. In that viewController
I need to check which button was clicked. So actually I need to check which segue was used/preformed. How can I check this in the viewControllers class? I know there is the prepareForSegue
method, but I cannot use this for my purpose because I need to put the prepareForSegue
in the class where the 2 buttons are, and I don't want it there but I want it in the viewControllers
class because I need to access and set some variables in that class.
Present Modally. This segue displays the view controller modally using the specified presentation and transition styles. The view controller that defines the appropriate presentation context handles the actual presentation.
What is an unwind segue and how does it work? An unwind segue lets you jump to any view controller further up your view controller hierarchy, destroying all view controllers preceding the destination. Before actually creating one, let's overview how an unwind segue works.
The first step is to find the “Storyboard Reference” item in our library and add it to the storyboard we want to segue from. You'll also want to add the Storyboard name and View Controller's Storyboard ID that you want to segue to in the Attributes Inspector of the Storyboard Reference.
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.
You need to set a variable of the second viewcontroller in the prepareforsegue method of first one. This is how it is done:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:segueIdentifier1])
{
SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
if(sender.tag == ...) // You can of course use something other than tag to identify the button
{
secondVC.identifyingProperty = ...
}
else if(sender.tag == ...)
{
secondVC.identifyingProperty = ...
}
}
}
Then you can check that property in the second vc to understand how you came there. If you have created 2 segues in the storyboard for 2 buttons, then only segue identifier is enough to set the corresponding property value. Then code turns into this:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:segueIdentifier1])
{
SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
secondVC.identifyingProperty = ...
}
else if([segue.identifier isEqualToString:segueIdentifier2])
{
SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
secondVC.identifyingProperty = ...
}
}
So firstly you need to set your segues identifier directly in storyborads or through your code using the performSegueWithIdentifier
method.
Independently the way you choosed, your view controller will fire the following method, so you need to override it to know which segue
was sending the message, you do like this:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ButtonSegueIdentifierOne"]) {
// button 1
}
if ([segue.identifier isEqualToString:@"ButtonSegueIdentifierTwo"]) {
// button 2
}
}
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