“口袋郴州”項目總結-NSNotificationCenter

來源:互聯網
上載者:User

標籤:io   ar   os   sp   strong   on   log   bs   ad   

1.發送中心,在要發出通知訊息的地方

[[NSNotificationCenter defaultCenter] postNotificationName:@"Firstnotification" object:urlstr2];

 參數:

         postNotificationName:通知的名字,也是通知的唯一標示,編譯器就通過這個找到通知的。

                 object:傳遞的參數

2.添加觀察者,接受通知的地方

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recievenotification:) name:@"Firstnotification" object:nil];

 參數介紹:

          addObserver: 觀察者,即在什麼地方接收通知;

        selector: 收到通知後調用何種方法;

        name: 通知的名字,也是通知的唯一標示,編譯器就通過這個找到通知的。

發送通知:調用觀察者處的方法。

3.調用方法

- (void)recievenotification:(NSNotification *)sender{
    currenturl = [NSURL URLWithString:[sender object]];//擷取到傳遞的對象
    
    NSLog(@"%@",currenturl);
    
}

4.你的觀察者如果對一些事件沒興趣了,應該從 NotificationCenter 中移除掉:

- (void)dealloc
{
    //移除觀察者
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Firstnotification" object:nil];
}

附:註冊鍵盤升啟關閉訊息

    1. //鍵盤升起 
    2. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    3. //鍵盤降下
    4. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

“口袋郴州”項目總結-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.