通知時(shí)碰到的問(wèn)題&&傳值

    view.selectNumberTF.text = [NSString stringWithFormat:@"%@",xYGoodsInfo.qidingCount];
    
    //增加通知中心監(jiān)聽(tīng),當(dāng)鍵盤(pán)出現(xiàn)或消失時(shí)收出消息
    [[NSNotificationCenter defaultCenter] addObserver:view selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:view selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    return view;
}

observer是實(shí)例的view,而不是self,self的話(huà)是指類(lèi)了,本來(lái)統(tǒng)一用self(類(lèi))也行,但是

- (void)keyboardWillShow:(NSNotification *)notification{
    //取得鍵盤(pán)最后的frame
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = keyboardFrame.origin.y;
  
    
    if([_delegate respondsToSelector:@selector(keyboardWillShow:)]){
        [_delegate keyboardWillShow:height];
    }
}

這個(gè)是實(shí)例方法,如果用self(類(lèi)) 不用view(實(shí)例)會(huì)閃退,
reason: '+[XYOtherIsNOValueView keyboardWillShow:]: unrecognized selector sent to class 0x1111dd010'

類(lèi)里沒(méi)這個(gè)方法(因?yàn)樗菍?shí)例的),
就算可以把keyboardWillShow改成類(lèi)方法,但是里面的delegate不能放到類(lèi)方法里面,

8@}6K2VL(5%W)99UA796P3O.jpg

所以還是乖乖用實(shí)例來(lái)通知吧

ps:

- (void)dealloc{
    //
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

self寫(xiě)在類(lèi)方法里代表類(lèi),寫(xiě)在實(shí)例方法里代表實(shí)例。


傳值

iOS--《傳值方法》之通知中心傳值

ps:記得移除觀察者

- (void)dealloc{
    //
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

貌似只有觀察者才有移除一說(shuō),發(fā)布的沒(méi)有

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容