有没有办法使用带有 UIKeyboardDidShowNotification 的 NSNotificationCenter 将附加数据传递给 UserInfo 字典

Is there a way to pass additional data to UserInfo dictionary using NSNotificationCenter with UIKeyboardDidShowNotification

我有这些代码行:

-(void)someMethod:(UIScrollView*)scrollView{
 [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(keyboardWasShown:)
     name:UIKeyboardDidShowNotification object:nil];
}
-(void)keyboardWasShown:(NSNotification *)aNotification{
// I want to get scrollView over here
}

有没有办法让这成为可能?我已尝试根据

执行此工作人员

How to pass a NSDictionary with postNotificationName:object:

但是aNotification.userInfo不包含我的数据

  [[NSNotificationCenter defaultCenter] 
postNotificationName:UIKeyboardDidShowNotification 
object:self
 userInfo:@{@"scrollView":scrollView}];

你正在听 UIKeyboardDidShowNotification 然后你 post UIKeyboardWillHideNotification 那是行不通的。首先,也许您喜欢创建自己的通知名称,即 XYZKeyboardDidShowNotification,然后注册该名称,然后 post 使用该名称和您想要的其他数据。然后你可以这样做:

-(void)keyboardWasShown:(NSNotification *)aNotification{
    UIScrollView *scrollView = aNotification.userInfo[@"scrollView"];
}