通知时碰到的问题&&传值

    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不能放到类方法里面,

通知时碰到的问题&&传值_第1张图片
8@}6K2VL(5%W)99UA796P3O.jpg

所以还是乖乖用实例来通知吧

ps:

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

self写在类方法里代表类,写在实例方法里代表实例。


传值

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

ps:记得移除观察者

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

貌似只有观察者才有移除一说,发布的没有

你可能感兴趣的:(通知时碰到的问题&&传值)