標籤:觀察者 簡訊 電話 網頁 中心
通知中樞的使用
第一個頁面三部;
-
產生一個通知中樞的對象(系統的一個單例類)
-
在通知中樞註冊一個觀察者
-
收到通知後調用的方法
-
dealloc移除觀察者的身份
第二個頁面
-
發送通知中樞
-
建立一個字典
-
返回你要的資料
直接跳轉的功能
//直接跳入的功能; //打電話 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://110"]]; //發簡訊 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://1010"]]; //郵件 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mail://[email protected]"]]; //開啟網頁 [[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];}
本文出自 “小劉_Blog” 部落格,請務必保留此出處http://liuyafang.blog.51cto.com/8837978/1557029
通知中樞的只用NSNotificationCenter傳值 和打電話,發簡訊,郵件,開啟網頁的直接開啟的功能