I have a DetailViewController, which is pushed/entered from RootViewController1 OR RootViewController2 through storyboard segues when Cell is selected.
The DetailViewController have a button with an IBAction. Can I program the action like:
If parent ViewController is RootViewController2, return. Else, perform Action.
Something like this:
-(IBAction)actionButtonPressed:(id)sender
{
if (parentViewController == RootViewController2) {
return;
}
//Else this is done:
textLabel.text = @"Test";
}
But I’m not sure how to make it work, an example of this would be great. Let me know if you need more info!
EDIT:
Code now looks like this:
#import "RootViewController2.h"
...
-(IBAction)actionButtonPressed:(id)sender
{
if([self.parentViewController isKindOfClass:[RootViewController2 class]]) {
return;
}
//Else this is done:
textLabel.text = @"Test";
}
But action is still performed from both views. Further suggestions?
Just as @Matt mentioned, the view controllers are stored in an array. You can access the navigation controller array like this:
regardless of how you obtain a reference to the parent, you still need to have a way to compare that reference. You could use introspection like this if ([object isKindOfClass:MyClass class]). But somehow in that approach (using the view controller arrays) you need an object reference to each parent objects. Its a tricky way to go.
Another possibly easier approach is to set a property value in the view controller before you segue to it in -(void)prepareForSegue …. block. Something like this:
and in the other segue from the other view controller
Now, when your button is pushed, you can just for against those values and then do the correct thing.
Not knowing your specific needs, I’d recommend the latter and advise against trying to use the viewcontrollers array.
hope that helps.