iPhone:孩子如何告诉父母做某事 [英] iPhone: How can a child tell his parent to do something

查看:98
本文介绍了iPhone:孩子如何告诉父母做某事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个RootViewController.m并在那里定义了一个方法:

OK, I have a RootViewController.m and defined a method in there:

-(void)doSomethingParent
{
    NSLog(@"Parent is doing something after child has asked");
}

然后我像这样添加一个childViewController.view:

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];

现在,我认为如果可以从孩子那里调用doSomethingParent方法,这确实非常有用.所以我想我会这样:

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];

}

但是xCode告诉我找不到"-doSomethingParent" ...但是我在其中放了@class?它找不到吗?我不想导入整个东西,因为我认为@class足以让孩子知道父母有一个叫做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!

推荐答案

将rootViewController对象设置为childViewController类型的对象的委托.并使用ChildViewController中的委托将消息传递到RootViewController.

Set rootViewController object as the delegate of an object of childViewController type. And use that delegate from ChildViewController to pass messages to RootViewController.

在RootViewController内部,当您创建ChildViewController对象时,请执行以下操作:

Inside RootViewController, when you create your ChildViewController object do:

childViewController.delegate = self;

在ChildViewController中,当您想传递消息时,将其传递给RootViewController,如下所示:

And in ChildViewController, when you want to pass a message then pass it to RootViewController as:

[delegate doSomething];

在ChildViewController.h接口中,声明一个ivar:

In your ChildViewController.h interface, declare an ivar:

id delegate;

然后将@property(在.h中)和@synthesize(在.m中)用于吸气剂和塞特剂.

Then use @property (in .h) and @synthesize (in .m) for getter and setter.

然后执行以上操作.

这篇关于iPhone:孩子如何告诉父母做某事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆