iPhone:孩子如何告诉父母做某事

发布于 2024-12-06 00:00:44 字数 963 浏览 0 评论 0原文

好的,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泅渡 2024-12-13 00:00:44

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

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

childViewController.delegate = self;

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

[delegate doSomething];

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

id delegate;

然后使用 @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:

childViewController.delegate = self;

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

[delegate doSomething];

In your ChildViewController.h interface, declare an ivar:

id delegate;

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

Then do the above.

东京女 2024-12-13 00:00:44

super 是对其超类的引用,而不是父视图控制器。您必须保留对 RootViewController 的引用并将消息传递给它......

或者编写协议并将您的 RootViewController 设置为子级的委托,因此子级将能够将消息传递给RootViewController。

我更喜欢第二种方式。

super is a reference to it's superclass, not the parent view controller. You will have to keep a reference to RootViewController 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 to RootViewController.

I prefer the second way.

靑春怀旧 2024-12-13 00:00:44

简单的方法(也许不是最好的方法):

self.child = cvc;
cvc.myParent = self;  // give the child a reference to its parent

Easy way (perhaps not the best way):

self.child = cvc;
cvc.myParent = self;  // give the child a reference to its parent
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文