您的位置:首页 > 其它

通知中心的只用NSNotificationCenter传值 和打电话,发短信,邮件,打开网页的直接打开的功能

2014-09-22 17:35 267 查看
通知中心的使用
第一个页面三部;
产生一个通知中心的对象(系统的一个单例类)

在通知中心注册一个观察者

收到通知后调用的方法

dealloc移除观察者的身份

第二个页面

发送通知中心

创建一个字典

返回你要的数据

直接跳转的功能

//直接跳入的功能;
//打电话
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://110"]];
//发短信
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://1010"]];
//邮件
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mail://110@110.gov"]];
//打开网页
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];
通知中心

第一个页面
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
self.title = @"第一页";
//通知中心的使用
//产生一个通知中心的对象 (系统的一个单例类)
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

//在通知中心注册一个观察者
//参数1:观察者
//参数2:观察者收到通知后  要做的事情
//参数3:观察什么事情 (一字符串标记)
//参数4:  限定发出通知的对象
[center addObserver:self selector:@selector(suibian:) name:@"我好困啊,想睡觉了____99" object:nil];

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
[button setTitle:@"按钮" forState:UIControlStateNormal];
button.backgroundColor = [UIColor blackColor];
[self.view addSubview:button];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[button release];

}
//收到通知后调用的方法
- (void)suibian:(NSNotification *)noti
{
self.view.backgroundColor = noti.object;  //接收改变收的颜色
NSLog(@"object:%@",noti.object);
NSLog(@"userInfo%@",noti.userInfo);
NSLog(@"收到通知");

}
- (void)dealloc
{
//注册为观察者的对象,需要在被系统dealloc之前,取消掉自己的观察者身份
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
第二个页面
- (void)buttonClick:(UIButton *)button
{
//    FirstViewController *first = [[FirstViewController alloc] init];

//通知中心的使用

//发送通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//参数1:给那个时间发送通知
//参数2:发送的通知可以带一个参数
//参数3:如果要传多个参数,就拼一个字典,填入第三个参数中
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"valur", @"key", nil];
UIColor *color = [UIColor darkGrayColor]; //要返回的颜色
[center postNotificationName:@"我好困啊,想睡觉了____99" object:/*@"附带参数,麻痹,困死啦"*/color userInfo:dic];

[self.navigationController popToRootViewControllerAnimated:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐