Ios 如何知道哪个对象发送了通知
当键盘出现时,我需要在Ios 如何知道哪个对象发送了通知,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,当键盘出现时,我需要在UIScrollView中移动UI元素,以便用户可以看到它 要实现此行为,我调用以下方法: - (void)registerForKeyboardNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)
UIScrollView
中移动UI元素,以便用户可以看到它
要实现此行为,我调用以下方法:
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)deregisterFromKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
在视图中将出现:
和视图将消失:
方法
但是,在我的例子中,同一个视图控制器上有两个文本视图,但是如果只有其中一个,我需要移动UI元素。我怎么做?我可以只为一个对象调用addObserver
,还是检查哪个对象调用它
提前感谢。您可以询问文本视图是否具有键盘焦点:
if ([myTextView1 isFirstResponder]) {
// Do this
} else if ([myTextView2 isFirstResponder]) {
// Do that.
}
将您的通知对象或用户信息传递给发件人
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;