您的位置:首页 > 移动开发 > IOS开发

IOS 通知传值

2015-10-15 20:07 344 查看
通知 是在跳转控制器之间通常用的传值代理方式 ,除了代理模式之外。 通知更加方便,便捷。是后面页面向前一个页面传值时使用。

先写后面传值页面 也就是发送通知页面

// 通常在点击方法中进行发送通知   使用NSNotification 进行发送通知
// 初始化 一个字典
NSDictionary *dic = @{@"xingming":@"lisi", @"xuehao":@"12345"};
//  创建通知
NSNotification *notification = [NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dic]; // 通过传递字典   再通过key值取值

// 发送通知
[[NSNotificationCenter defaultCenter] postNotification:nottification];

// 返回上一页
[self.navigationController popTopRootViewControllerAniamted:YES];
//  这样就把通知发出去了   只要在  上一个页面  获取到通知   并且从通知中通过key值获取值


前一个页面 也是接收通知页面

//  在viewDidload 中  注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongtong:) name@"tongzhi" object:nil];

// 实现方法 tongtong
- (void)tongtong:(NSNotification *)text
{
NSLog(@"%@", text.userInfo[@"xingming"]);
NSLog(@"成功接到通知!");

// 2015-10-15 20:05:10.164 通知[4936:397345] lisi
// 2015-10-15 20:05:10.165 通知[4936:397345] 接到通知
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: