Yoh*_* T. 4 iphone model-view-controller cocoa-touch uiviewcontroller
我还没想到:我有一个mainViewController可以切换两个视图,viewControllerA和ViewControllerB.我切换视图的方法是在mainViewController中使用UIButton(mainButton),然后单击它可以切换viewControllerA < - > ViewControllerB.
现在这是我的问题.我的ViewControllerA有一个UIButton(ButtonA).我希望通过点击它,它告诉mainViewController切换到另一个视图(viewControllerB)
换句话说,子视图(viewControllerA)应该向mainViewController(其父视图)发送一条消息,它要触发属于主视图的方法,而不是它自己(viewA).
我怎么能实现这个目标呢?
Cor*_*oyd 15
与父对象的通信时,您可以选择一些设计模式.授权和通知都是不错的选择.
这里的重要思想是松散耦合的沟通.通知使用Singleton来处理通信,而委托使用对父对象的弱引用.(查看Cocoa With Love:保留周期)
如果你使用委托,你可以为你的ViewControllerA创建一个MainViewController必须符合的非正式协议.
您可以将其称为ViewControllerADelegate协议:
@protocol ViewControllerADelegate
@optional
- (void)bringSubViewControllerToFront:(UIViewController*)aController;
@end
Run Code Online (Sandbox Code Playgroud)
或者ViewControllerA可以发布通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyFunkyViewSwitcherooNotification" object:self];
Run Code Online (Sandbox Code Playgroud)
如果要知道MainViewController应该是持久的:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(swapThoseViews:) name:@"MyFunkyViewSwitcherooNotification" object:nil];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4834 次 |
| 最近记录: |