Iphone 一次向多个对象发送消息(objective-c)
(或使用一个值设置多个对象) 是否有一种方法可以在一行中发送多个对象和一条消息 很像Iphone 一次向多个对象发送消息(objective-c),iphone,objective-c,object,messaging,Iphone,Objective C,Object,Messaging,(或使用一个值设置多个对象) 是否有一种方法可以在一行中发送多个对象和一条消息 很像 [someObject, otherObject reset]; 就像在LUA脚本(我相信是C?)中一样,您可以设置多个对象: someThing, otherThing = 1 , 0 不是真的。这是Lua(而不是Lua)和Matlab的一个特殊特性 您可以考虑使用NSnNoTrimeCaseCo并以这样的方式向多个对象发送消息,但这是更多的工作。 简而言之,不,ObjuleC和C都不支持这个特性。作为一
[someObject, otherObject reset];
就像在LUA脚本(我相信是C?)中一样,您可以设置多个对象:
someThing, otherThing = 1 , 0
不是真的。这是Lua(而不是Lua)和Matlab的一个特殊特性
您可以考虑使用NSnNoTrimeCaseCo并以这样的方式向多个对象发送消息,但这是更多的工作。
简而言之,不,ObjuleC和C都不支持这个特性。作为一种极端度量,您可以使用
-[NSArray makeObjectsPerformSelector:
和-[NSArray makeObjectsPerformSelector:withObject:
,例如
[[NSArray arrayWithObjects:someObject, otherObject, nil] makeObjectsPerformSelector:@selector(reset)];
我可能会使用一个通知
NSDictionary *messages = [NSDictionary dictionaryWithObjectsAndKeys:@"hello", @"object 1", @"bye", @"object2", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"reset" object:message];
NSDictionary *receivedMessage = [theNotification object];
您需要将这些对象订阅到您的通知并发送它。这两个对象都将收到通知
NSDictionary *messages = [NSDictionary dictionaryWithObjectsAndKeys:@"hello", @"object 1", @"bye", @"object2", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"reset" object:message];
NSDictionary *receivedMessage = [theNotification object];
例如,如果对象是ViewController,则可以将此位添加到其viewDidLoad方法中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reset:) name:@"reset" object:nil];
metod重置:必须采用以下形式:
- (void)reset:(NSNotification *)theNotification;
然后,当您希望将消息发送到所有这些对象时,您会发布通知
NSDictionary *messages = [NSDictionary dictionaryWithObjectsAndKeys:@"hello", @"object 1", @"bye", @"object2", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"reset" object:message];
NSDictionary *receivedMessage = [theNotification object];
因此,每个对象将接收字典消息并执行方法重置
为了将该方法用作字典,您必须从通知中获取对象
NSDictionary *messages = [NSDictionary dictionaryWithObjectsAndKeys:@"hello", @"object 1", @"bye", @"object2", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"reset" object:message];
NSDictionary *receivedMessage = [theNotification object];
另外,不要忘记从通知中心删除这些对象。我在他们的dealloc方法中使用此位:
[[NSNotificationCenter defaultCenter] removeObserver:self];
NSArray中有两种方法可以帮助实现这一点:
- (void)makeObjectsPerformSelector:(SEL)一个选择器
- (void)makeObjectsPerformSelector:(SEL)一个带有Object:(id)anObject的选择器