调用父视图控制器(通过navigationcontroller) [英] Call a parent view controller (through a navigationcontroller)
问题描述
目前我使用 pushViewController:animated:
添加一个viewcontroller,现在从那个新的想要在我的parent控制器中调用一个方法。我想我会遇到导航控制器。
Currently I add a viewcontroller using pushViewController:animated:
and now from within that new one would like to call a method inside my "parent"-controller. I think I get stuck at the navigationcontroller.
目前正在尝试这个以查看它是否是我想要的控制器:
Currently trying this to see if it's the controller I want:
if([self.superclass isKindOfClass:[MySuperController class]])
// and tried:
if([self.presentingViewController isKindOfClass:[MySuperController class]])
这两个都不起作用。
如何访问推送当前控制器的控制器(其中的方法)?
How can I access the controller (a method in it) that pushed the current one?
推荐答案
就像Marsson提到的那样,你需要使用委托...
Just like Marsson mentioned, you need to use delegate...
以下是一个示例:
在您的子视图控制器.h文件中:
In your child view controller .h file:
@protocol ChildViewControllerDelegate <NSObject>
- (void)parentMethodThatChildCanCall;
@end
@interface ChildViewController : UIViewController
{
}
@property (assign) id <ChildViewControllerDelegate> delegate;
在您的子视图控制器.m文件中:
In your child view controller .m file:
@implementation ChildViewController
@synthesize delegate;
// to call parent method:
// [self.delegate parentMethodThatChildCanCall];
在父视图控制器.h文件中:
In parent view controller .h file:
@interface parentViewController <ChildViewControllerDelegate>
在父视图控制器.m文件中:
In parent view controller .m file:
//after create instant of your ChildViewController
childViewController.delegate = self;
- (void) parentMethodThatChildCanCall
{
//do thing
}
这篇关于调用父视图控制器(通过navigationcontroller)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!