n.e*_*ind 1 iphone model-view-controller cocoa-touch objective-c parent-child
好的,我有一个RootViewController.m并在那里定义了一个方法:
-(void)doSomethingParent
{
NSLog(@"Parent is doing something after child has asked");
}
Run Code Online (Sandbox Code Playgroud)
然后我添加了一个childViewController.view,如下所示:
if (self.child == nil) {
ChildViewController *cvc = [[ChildViewController alloc]
initWithNibName:nil bundle:nil];
self.child = cvc;
[cvc release];
}
[self.view insertSubview: child.view atIndex:0];
Run Code Online (Sandbox Code Playgroud)
现在,我认为如果我可以从孩子那里调用我的doSomethingParent方法,那将非常有用.所以我想我会这样做:
@implementation ChildViewController
@class RootViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[super doSomethingParent];
}
Run Code Online (Sandbox Code Playgroud)
但是xCode告诉我"找不到-doSomethingParent"......但我把@class放在那里?!不应该找到它吗?我不想导入整个东西,因为我认为@class足以让孩子知道父母有一个名为doSomethingParent的方法...
我会非常感谢任何建议.提前致谢!
将rootViewController对象设置为childViewController类型的对象的委托.并使用来自ChildViewController的委托将消息传递给RootViewController.
在RootViewController内部,当您创建ChildViewController对象时,执行以下操作:
childViewController.delegate = self;
Run Code Online (Sandbox Code Playgroud)
在ChildViewController中,当您想要传递消息时,将其传递给RootViewController,如下所示:
[delegate doSomething];
Run Code Online (Sandbox Code Playgroud)
在ChildViewController.h界面中,声明一个ivar:
id delegate;
Run Code Online (Sandbox Code Playgroud)
然后使用@property(在.h)和@synthesize(在.m中)获取getter和setter.
然后做上面的事情.
归档时间: |
|
查看次数: |
1264 次 |
最近记录: |