iOS 简单使用通知模式
2013-05-03 14:57
351 查看
今天在写了一个天气预报模块,切换城市时,要传值给显示天气的controller里,选择城市有一级和二级界面,二级界面使用代理不好实现,用到了通知模式,也就是
NSNotificationCenter类。
首先我在二级controller的cell上触发点击事件代码如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"点击二级页面 %d",indexPath.row);
[self.navigationController
popToViewController:[self.navigationController.viewControllers
objectAtIndex:1]
animated:YES];
NSNotificationCenter *nc = [NSNotificationCenter
defaultCenter];
[nc
postNotificationName:@"District"
object:self
userInfo:[self.dataArray
objectAtIndex:indexPath.row]];
}
我这个时传的一个城市代码;
在天气controlloer里利用这个城市代码参数,重新请求url;下面是接受通知的代码:
- (void)viewWillAppear:(BOOL)animated{
[super
viewWillAppear:animated];
self.navigationController.navigationBar.tintColor = [UIColor
blackColor];
self.navigationItem.title=@"天气预报";
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(recvBcast:)
name:@"District"
object:nil];
}
- (void)recvBcast:(NSNotification*)noti
{
//获取所传的参数
NSDictionary *dict1 = (NSDictionary*)[noti
userInfo];
code = [dict1 objectForKey:@"code"];
[[NSUserDefaults
standardUserDefaults] setObject:code
forKey:@"cityCode"];
//这个是长期保存数据的
[[NSUserDefaults
standardUserDefaults] synchronize];
NSLog(@"code
是 %@",code);
[self
loadweather_now];
[self
loadWeatherData];
}
不写[[NSUserDefaults standardUserDefaults] synchronize]会出现数据丢失。
最后是把这个通知移除,这里有个注意的点,就是要把移除的方法放到
- (void)viewWillDisappear:(BOOL)animated
放进deollc里是没用的。这个通知是系统唯一的,销毁不了,只能移除。
新手求上进,不对的地方大神门请指点。
NSNotificationCenter类。
首先我在二级controller的cell上触发点击事件代码如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"点击二级页面 %d",indexPath.row);
[self.navigationController
popToViewController:[self.navigationController.viewControllers
objectAtIndex:1]
animated:YES];
NSNotificationCenter *nc = [NSNotificationCenter
defaultCenter];
[nc
postNotificationName:@"District"
object:self
userInfo:[self.dataArray
objectAtIndex:indexPath.row]];
}
我这个时传的一个城市代码;
在天气controlloer里利用这个城市代码参数,重新请求url;下面是接受通知的代码:
- (void)viewWillAppear:(BOOL)animated{
[super
viewWillAppear:animated];
self.navigationController.navigationBar.tintColor = [UIColor
blackColor];
self.navigationItem.title=@"天气预报";
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(recvBcast:)
name:@"District"
object:nil];
}
- (void)recvBcast:(NSNotification*)noti
{
//获取所传的参数
NSDictionary *dict1 = (NSDictionary*)[noti
userInfo];
code = [dict1 objectForKey:@"code"];
[[NSUserDefaults
standardUserDefaults] setObject:code
forKey:@"cityCode"];
//这个是长期保存数据的
[[NSUserDefaults
standardUserDefaults] synchronize];
NSLog(@"code
是 %@",code);
[self
loadweather_now];
[self
loadWeatherData];
}
不写[[NSUserDefaults standardUserDefaults] synchronize]会出现数据丢失。
最后是把这个通知移除,这里有个注意的点,就是要把移除的方法放到
- (void)viewWillDisappear:(BOOL)animated
放进deollc里是没用的。这个通知是系统唯一的,销毁不了,只能移除。
新手求上进,不对的地方大神门请指点。
相关文章推荐
- iOS监听模式系列之NSNotificationCenter的简单使用
- iOS监听模式系列之NSNotificationCenter的简单使用
- 【iOS开发】观察者模式-通知的使用及深入理解
- iOS 观察者模式(KVO)的简单使用
- iOS 通知(NSNotification)的简单使用
- ios通知使用 书上案例 简单易懂
- iOS开发中单例设计模式、通知中心的使用
- [iOS]监控手机虚拟键盘弹出,消失的通知简单使用
- ios NSNotificationCenter通知的简单使用
- iOS通知的简单使用
- iOS开发UI篇章使用UItableview完成一个简单的QQ好友列表(一)
- iOS-AVPlayer的简单使用
- 【代码笔记】iOS-只让textField使用键盘通知
- IOS怎么使用IB编写简单的应用
- iOS 使用纯代码自定义UITableViewCell实现一个简单的微博界面布局
- iOS开发的2D绘制--CoreGraphics的简单使用一(画线条)
- iOS开发技巧(系列十七:使用Xcode DEBUG模式和RELEASE模式)
- 【iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- 简单工厂模式——接口使用
- iOS UICollectionView简单使用