iphone – 如何使用NSNotificationcenter的object属性

iphone – 如何使用NSNotificationcenter的object属性,第1张

概述可能有人请告诉我如何使用NSNotifcationCenter上的object属性。我想能够使用它传递一个整数值到我的选择器方法。 这是我在我的UI视图中设置通知监听器。看到我想要一个整数值传递我不知道什么替换nil。 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent:) n 可能有人请告诉我如何使用NSNotifcationCenter上的object属性。我想能够使用它传递一个整数值到我的选择器方法。

这是我在我的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属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1047846.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存