这是我在我的UI视图中设置通知监听器。看到我想要一个整数值传递我不知道什么替换nil。
[[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(receiveEvent:) name:@"myevent" object:nil];- (voID)receiveEvent:(NSNotification *)notification { // handle event NSLog(@"got event %@",notification);}
我从这样的另一个类调度通知。该函数传递一个名为index的变量。这个值,我想以某种方式触发与通知。
-(voID) disptachFunction:(int) index{ int pass= (int)index; [[NSNotificationCenter defaultCenter] postNotificationname:@"myevent" object:pass]; //[[NSNotificationCenter defaultCenter] postNotificationname:<#(Nsstring *)aname#> object:<#(ID)anObject#>}解决方法 object参数表示通知的发送者,通常是self。
如果您想传递额外的信息,您需要使用NSNotificationCenter方法postNotificationname:object:userInfo :,它接受一个任意值的字典(您可以自由定义)。内容需要是实际的NSObject实例,而不是一个整数类型,如整数,所以你需要用NSNumber对象包装整数值。
NSDictionary* dict = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt:index] forKey:@"index"];[[NSNotificationCenter defaultCenter] postNotificationname:@"myevent" object:self userInfo:dict];总结
以上是内存溢出为你收集整理的iphone – 如何使用NSNotificationcenter的object属性全部内容,希望文章能够帮你解决iphone – 如何使用NSNotificationcenter的object属性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)