我在 xcode 中创建新的选项卡式视图项目,在 appdelegate 中我创建了一个协议
.h file
@protocol myProtocol <NSObject>
-(void)myProtocolMethodOne;
@end
.
.
.
@property (weak) id<myProtocol> mypDelegate;
.m file
@synthesize mypDelegate;
.
.
.
//Inside didFinishLaunchingWithOptions
[mypDelegate myProtocolMethodOne];
在firstViewController和secondViewController中(两者都显示为两个不同的选项卡)我在两者中都这样做了
AppDelegate *ad = (AppDelegate*)[[UIApplication sharedApplication]delegate];
[ad setMypDelegate:self];
.
.
.
-(void)myProtocolMethodOne
{
NSLog(@"1st VC");
[[self tabBarItem]setBadgeValue:@"ok"];
}
代码工作正常,但只有 secondaryViewController 响应。
我正在寻找一种使用委托而不是通知的广播和监听器类型的机制。
我搜索了很多,但确实找到了任何解决方案,除了this https://github.com/robbiehanson/XMPPFramework/wiki/MulticastDelegate但代码对我来说是提前理解的,所以我通过从一个简单的项目开始逐步理解这一点。请帮助我解决这个问题。两个视图控制器如何同时响应委托,我该怎么办?
您可以考虑类似于访问者模式的东西,而不是委托。
@interface MyVisitor : NSObject < myProtocol >
-(void)addAcceptor:(id < myProtocol >)acceptor
@end
@implementation
-(void)myProtocolMethodOne {
[_acceptors enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *break){
[obj performSelector:_sel];
}];
}
// etc etc ... obviously you have to handle return values if you're getting these
@end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)