当前位置: 代码迷 >> Iphone >> Iphone 不同页面间传接值 对象
  详细解决方案

Iphone 不同页面间传接值 对象

热度:351   发布时间:2016-04-25 06:27:20.0
Iphone 不同页面间传递值 对象
这个问题折腾了我几天。。这就是初学要付出的代价吧。。。我的需求很简单,就是在不同的页面间传递值,a页面跳转到b页面,b页面作出选择后需要把值传递给a,开始用delegate,瞎折腾了一气没有成功,后来了解到可以通过NSNotification传值,测试后成功,非常好用,具体代码如下:
先在b页面合适的位置定义一个notification,然后发送notification:
NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self];[[NSNotificationCenter defaultCenter] postNotification:notification];

然后在a页面合适的位置接收:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aNotificationHandler:) name:@"MyNotification" object:nil];

定义一个方法处理notification:
- (void)aNotificationHandler:(NSNotification*)notification{	MyNotifyingClass* ob = [notification object];	... app specific stuff}

以上代码参考国外博客中的写法,原文链接如下:
http://blog.isotoma.com/2009/11/on-objective-c-delegates-and-nsnotification-objects/

  相关解决方案