简体   繁体   中英

Tell which ViewController pushed the DetailViewController

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?

The view controllers pushed onto a UINavigationController form a stack (a linear sequence). You can fetch this stack as the UINavigationController's viewControllers array. (You can refer to the UINavigationController itself as a pushed view controller's navigationController .) Since this detail controller is at the top of the stack (the end of the stack), the one you want to know about is the previous one - the next-to-last item of the array.

Just as @Matt mentioned, the view controllers are stored in an array. You can access the navigation controller array like this:

[self.navigationController.viewControllers lastobject ]
[self.navigationController.viewControllers objectAtIndex:2];

You can also get a reference to the parent view controller like this:
self.parentViewController
self.presentingViewController

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:

 MyClass *myClass = segue.destinationViewController;
 myClass.myLogicProperty = @"mommy";

and in the other segue from the other view controller

MyClass *myClass = segue.destinationViewController;
 myClass.myLogicProperty = @"daddy";

Now, when your button is pushed, you can just for against those values and then do the correct thing.

if ([self.myLogicProperty isEqualTo:@"daddy"]) {
   //take action
}
else if ([self.myLogicProperty isEqualTo:@"mommy"]) {
   //take action
}
else {
//do something if no match
}

Not knowing your specific needs, I'd recommend the latter and advise against trying to use the viewcontrollers array.

hope that helps.

#import "RootViewController2.h"

...

-(IBAction)actionButtonPressed:(id)sender
{
#define CurrentParentViewController [[self.navigationController viewControllers] objectAtIndex:[[self.navigationController viewControllers] count] - 2]
if([CurrentParentViewController isKindOfClass:[RootViewController2 class]]) {
    return;
}

//Else this is done:
textLabel.text = @"Test";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM