委托对象是如何被调用的?
我的协议:
@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)它应该被解雇。应该通过点击后退按钮来触发该解雇。对象如何完成这个通知?
my protocol:
@protocol ElectricalSystemEngineDelegate
-(void)didRequestMainMenu:(id)sender;
@end
I designed this protocol in order to handle dismissal of a modal View Controller inside my rootView controller. My rootView controller adopts this protocol and is declared as follows:
#import "ElectricalSystemEngineDelegate.h"
@interface RootViewController: UIViewController <ElectricalSystemEngineDelegate>
//other ivars & methods including instantiation of my modalViewController.
I use an out-of-the-box:
-(IBAction)displayElectricalViewController
-to display the modal controller... which works fine. I am, however, confused on how to proceed further with the implementation of this protocol to handle dismissal of the controller..
//The implementation of my root view controller.
-(void)didRequestMainMenu:(id)sender {
[self dismissModalViewControllerAnimated: YES];
}
Obviously, I correctly implemented the protocol by using the prescribed method. I want the method to dismiss my view controller when called. I also want it to be called by the tapping of a back button in the modalViewController.
As the apple docs say, "In some cases, an object might be willing to notify other objects of its actions so that they can take whatever collateral measures might be required." My goal is for my ElecticalViewController to notify its parent (the RootViewController) that it should be dismissed. That dismissal should be triggered by tapping the back button. How does the object accomplish this notification?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要添加
id; delegate
属性给您的 ElectricalViewController。然后,在创建 ElectricalViewController 后,您需要将 self (RootViewController) 分配给该委托。
然后在处置 ElectricalViewController 时调用 [delegate didRequestMainMenu]。
然后您需要为 RootViewController 创建一个方法 didRequestMainMenu。
You need to add
id <ElectricalSystemEngineDelegate> delegate
property to your ElectricalViewController.Then you need to assign self (RootViewController) to that delegate after you created ElectricalViewController.
Then you call [delegate didRequestMainMenu] when you dispose ElectricalViewController.
Then you need to create a method didRequestMainMenu to your RootViewController.