Ios 如何在类内为NSNotification选择选择器?
我有一个键盘助手类,希望调用Ios 如何在类内为NSNotification选择选择器?,ios,ios7,nsnotificationcenter,Ios,Ios7,Nsnotificationcenter,我有一个键盘助手类,希望调用键盘中定义的方法keyboardShow keyboard = [[FTKeyboardHelper alloc] init]; 但是,我收到错误消息例外: 有什么建议吗 怎么样 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboard.keyboardShow:
键盘
中定义的方法keyboardShow
keyboard = [[FTKeyboardHelper alloc] init];
但是,我收到错误消息例外:
有什么建议吗 怎么样
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboard.keyboardShow:)
name:UIKeyboardWillShowNotification
object:nil];
我不确定您是否可以“.”分隔选择器,它不是关键路径
但我认为这将是一个糟糕的设计,您可能应该将该代码放在助手中。
在
-init
中,并在-dealloc
中注销,或使用一对-register
和-unregister
方法将其控制在对象的正常生命周期之外。但是不要忘记在解除分配帮助程序之前取消注册,否则,下次键盘出现时,您将崩溃。+1关于将观察者放入键盘帮助程序类。我想那是个更好的主意。
[[NSNotificationCenter defaultCenter] addObserver:keyboard
selector:@selector(keyboardShow:)
name:UIKeyboardWillShowNotification
object:nil];