iPhone:孩子如何告诉父母做某事
好的,我有一个 RootViewController.m 并在其中定义了一个方法:
-(void)doSomethingParent
{
NSLog(@"Parent is doing something after child has asked");
}
然后我添加了一个 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];
现在,我认为如果我可以从子级调用我的 doSomethingParent 方法,这确实会非常有用。所以我想我会这样做:
@implementation ChildViewController
@class RootViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[super doSomethingParent];
}
但是xCode告诉我找不到“-doSomethingParent”...但我把@class放在那里?!不是应该找得到吗?我不想导入整个内容,因为我认为 @class 足以让孩子知道父母有一个名为 doSomethingParent 的方法...
我将非常感谢任何建议。提前致谢!
OK, I have a RootViewController.m and defined a method in there:
-(void)doSomethingParent
{
NSLog(@"Parent is doing something after child has asked");
}
I then added a childViewController.view like so:
if (self.child == nil) {
ChildViewController *cvc = [[ChildViewController alloc]
initWithNibName:nil bundle:nil];
self.child = cvc;
[cvc release];
}
[self.view insertSubview: child.view atIndex:0];
Now, I thought it would be very useful indeed if I could call my doSomethingParent method from the child. So I thought I would do it like this:
@implementation ChildViewController
@class RootViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[super doSomethingParent];
}
But xCode tells me that "-doSomethingParent" not found... but I put @class in there?! Shouldn't it find it? I don't want to import the whole thing as I thought @class would be sufficient to let the child know that the parent has a method called doSomethingParent...
I'd be very grateful for any suggestions. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 rootViewController 对象设置为 childViewController 类型的对象的委托。并使用 ChildViewController 中的委托将消息传递给 RootViewController。
在 RootViewController 内部,当您创建 ChildViewController 对象时,请执行以下操作:
在 ChildViewController 中,当您想要传递消息时,请将其传递给 RootViewController,如下所示:
在 ChildViewController.h 接口中,声明一个 ivar:
然后使用 @property (在 .h 中)并@synthesize(以 .m 为单位)用于 getter 和 setter。
然后执行上述操作。
Set rootViewController object as the delegate of an object of childViewController type. And use that delegate from ChildViewController to pass messages to RootViewController.
Inside RootViewController, when you create your ChildViewController object do:
And in ChildViewController, when you want to pass a message then pass it to RootViewController as:
In your ChildViewController.h interface, declare an ivar:
Then use @property (in .h) and @synthesize (in .m) for getter and setter.
Then do the above.
super 是对其超类的引用,而不是父视图控制器。您必须保留对 RootViewController 的引用并将消息传递给它......
或者编写协议并将您的 RootViewController 设置为子级的委托,因此子级将能够将消息传递给RootViewController。
我更喜欢第二种方式。
super
is a reference to it's superclass, not the parent view controller. You will have to keep a reference toRootViewController
and pass the message to it......Or code a protocol and set your
RootViewController
as delegate of the child, so the child would be able to pass messages toRootViewController
.I prefer the second way.
简单的方法(也许不是最好的方法):
Easy way (perhaps not the best way):