Objective-c 多播委托 [英] Objective-c multicasting delegates
问题描述
我在 xcode 中创建了新的选项卡式视图项目,在 appdelegate 中我创建了一个协议
I create new tabbed view project in xcode, in appdelegate I created a protocol
.h 文件
@protocol myProtocol <NSObject>
-(void)myProtocolMethodOne;
@end
.
.
.
@property (weak) id<myProtocol> mypDelegate;
.m 文件
@synthesize mypDelegate;
.
.
.
//Inside didFinishLaunchingWithOptions
[mypDelegate myProtocolMethodOne];
在 firstViewController &secondViewController(两者都显示为两个不同的选项卡)我在两者中都这样做了
In firstViewController & secondViewController (both are displayed as two different tab) I did this in both
AppDelegate *ad = (AppDelegate*)[[UIApplication sharedApplication]delegate];
[ad setMypDelegate:self];
.
.
.
-(void)myProtocolMethodOne
{
NSLog(@"1st VC");
[[self tabBarItem]setBadgeValue:@"ok"];
}
代码运行良好,但只有 secondViewController 有响应.
The code is working perfectly but only secondViewController is responding.
我正在寻找一种使用委托而不是通知的广播和侦听器机制.
I am looking for a broadcasting and listener kind of mechanism using delegates not notifications.
我搜索了很多,但确实找到了除this之外的任何解决方案,但代码已提前我理解,所以我通过从一个简单的项目开始,一步一步地理解这一点.请帮我解决这个问题.两个viewcontroller如何同时响应delegate,我该怎么办?
I searched a lot but did find any solutions except this but code is advance for me to understand, so I am taking a step by step approach to understand this by starting form a simple project. Please help me regarding this. How both viewcontrollers can respond to delegate at same time, what should I do?
推荐答案
在您的情况下,通过持有委托数组(可能作为私有属性)并允许添加/删除委托,足以容纳包含所有委托的数组:
In your case is enough to hold an array with all delegates, by holding an array of delegates, possibly as a private property, and allowing to add/remove delegates:
@interface AppDelegate() // .h file
@property (nonatomic,strong,readwrite) NSMutableArray* delegates;
@end
// .m file
- (void) addDelegate: (id<MyProtocol>) delegate // By convention the first letter should be capital.
{
// Optional code you may need to execute before adding it.
[delegates addObject: delegate];
}
我将 removeDelegate 方法留给您,它的实现非常简单.
I leave to you the removeDelegate method, it's very simple to implement.
如何调用委托方法
在每个对象上调用选择器就足够了:
It's enough to call the selector on every object:
[delegates makeObjectsPerformSelector: myProtocolMethodOne];
如果您需要获取返回值,最好这样做:
If you need to take return values it's better to do it this way:
NSMutableArray* returnValues= [NSMutableArray new];
for(id<MyProtocol> delegate in delegates)
{
id result= [delegate myProtocolMethodTwo]; // Method returning a value
[returnValues addObject: result];
}
如何添加代表
在每个控制器中(最多 N 次),您应该能够添加它:
In every controller (up to N times) you should be able to add it:
AppDelegate *ad = (AppDelegate*)[[UIApplication sharedApplication]delegate];
[ad addDelegate:self];
附加问题:您可能想要弱委托,但 NSArray 拥有强引用.在这里你可以找到一个很好的解决方案:
Additional problem: You may want to have weak delegates, but a NSArray holds strong references. Here you can find a nice solution for this:
ARC 下对象的弱引用 (__unsafe_unretained) 数组一个>
它基本上是说使用 NSValue 使用 valueWithNonretainedObject 方法来存储弱引用.
It basically says to use NSValue to store weak reference using the valueWithNonretainedObject method.
这篇关于Objective-c 多播委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!