2012-01-19 64 views

回答

2

它取决于哪些类被实例化以及如何形成实际的对象。这也取决于子类是否调用super来处理。否则,正如NSNotificationCenter的文档所说,接收通知的对象的顺序是随机的,并且不取决于您是否是子类或超类。考虑更好地理解下面的示例:(需要几个例子作为你的解释并不完全清楚):

例1:两个不同的对象

ParentClass *obj1 = [[ParentClass alloc] init]; 
ChildClass *obj2 = [[ChildClass alloc] init]; 
// register both of them as listeners for NSNotificationCenter 
// ... 
// and now their order of receiving the notifications is non-deterministic, as they're two different instances 

例2:子类调用super

@implementation ParentClass 

- (void) handleNotification:(NSNotification *)not 
{ 
    // handle notification 
} 

@end 

@ipmlementation ChildClass 

- (void) handleNotification:(NSNotification *)not 
{ 
    // call super 
    [super handleNotification:not]; 
    // actually handle notification 
    // now the parent class' method will be called FIRST, as there's one actual instace, and ChildClass first passes the method onto ParentClass 
} 

@end 
+0

通知操作方法必须具有一个参数,即通知对象。 –

+1

@JoshCaswell,这是真的吗?我认为如果您在注册您不需要参数时省略了选择器名称后面的冒号。我不能测试刚才... –

+0

@Josh Caswell nope。如果您根据需要声明一个参数较少的方法,这不是问题。由于C函数调用约定,调用时参数位于堆栈的顶部,并且函数可以一个接一个地弹出它们,直到它“用完参数”。所以唯一真正的问题是当你使用更多的参数。 – 2012-01-20 06:16:08