【问题标题】:How is a delegate object called?如何调用委托对象?
【发布时间】:2011-01-19 17:06:41
【问题描述】:

我的协议:

@protocol ElectricalSystemEngineDelegate
-(void)didRequestMainMenu:(id)sender;
@end

我设计此协议是为了处理我的 rootView 控制器中模态视图控制器的解除。我的rootView控制器采用了这个协议,声明如下:

#import "ElectricalSystemEngineDelegate.h"

@interface RootViewController: UIViewController <ElectricalSystemEngineDelegate>
//other ivars & methods including instantiation of my modalViewController.

我使用的是开箱即​​用的:

-(IBAction)displayElectricalViewController

-显示模态控制器...工作正常。但是,我对如何进一步执行此协议以处理控制器的解雇感到困惑..

//The implementation of my root view controller.
-(void)didRequestMainMenu:(id)sender {
    [self dismissModalViewControllerAnimated: YES];
}

显然,我使用规定的方法正确地实现了协议。我希望该方法在调用时关闭我的视图控制器。我还希望通过点击 modalViewController 中的后退按钮来调用它。

正如苹果文档所说,“在某些情况下,一个对象可能愿意将其行为通知其他对象,以便他们可以采取任何可能需要的附带措施。”我的目标是让我的 ElecticalViewController 通知其父级(RootViewController)它应该被解雇。应该通过点击后退按钮来触发解雇。对象如何完成这个通知?

【问题讨论】:

    标签: iphone objective-c delegation


    【解决方案1】:

    您需要将id &lt;ElectricalSystemEngineDelegate&gt; delegate 属性添加到您的ElectricalViewController。

    然后您需要在创建 ElectricalViewController 后将 self (RootViewController) 分配给该委托。

    然后在你处理 ElectricalViewController 时调用 [delegate didRequestMainMenu]。

    然后你需要为你的 RootViewController 创建一个方法 didRequestMainMenu。

    【讨论】:

    • 工作就像做梦一样。那是我遗漏的唯一一块拼图。谢谢!!
    猜你喜欢
    • 2018-11-11
    • 2017-04-21
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多