通知中樞的只用NSNotificationCenter傳值 和打電話,發簡訊,郵件,開啟網頁的直接開啟的功能

來源:互聯網
上載者:User

標籤:觀察者   簡訊   電話   網頁   中心   

通知中樞的使用

第一個頁面三部;

  1.     產生一個通知中樞的對象(系統的一個單例類)

  2. 在通知中樞註冊一個觀察者

  3. 收到通知後調用的方法

  4. dealloc移除觀察者的身份

第二個頁面 

  1. 發送通知中樞

  2. 建立一個字典

  3. 返回你要的資料


直接跳轉的功能


    //直接跳入的功能;    //打電話    [[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傳值 和打電話,發簡訊,郵件,開啟網頁的直接開啟的功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.