通知時碰到的問題&&傳值

    view.selectNumberTF.text = [NSString stringWithFormat:@"%@",xYGoodsInfo.qidingCount];
    
    //增加通知中心監聽,當鍵盤出現或消失時收出消息
    [[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是實例的view,而不是self,self的話是指類了,本來統一用self(類)也行,但是

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

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

類里沒這個方法(因為它是實例的),
就算可以把keyboardWillShow改成類方法,但是里面的delegate不能放到類方法里面,

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

所以還是乖乖用實例來通知吧

ps:

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

self寫在類方法里代表類,寫在實例方法里代表實例。


傳值

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

ps:記得移除觀察者

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

貌似只有觀察者才有移除一說,發布的沒有

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容