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寫在類方法里代表類,寫在實例方法里代表實例。
傳值
ps:記得移除觀察者
- (void)dealloc{
//
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
貌似只有觀察者才有移除一說,發布的沒有