Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何知道哪个对象发送了通知_Ios_Objective C_Cocoa Touch - Fatal编程技术网

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;