iOS - NSNotificationCenter multiple UIKeyboard notification

北城余情 提交于 2020-01-02 13:50:15

问题


I have two view controllers let's call them A and B

(1) in A I show a popOver containing a textField
(2) in B there is an UITextView used for simple text editing

I Have to manage the keyboard in A and in B to scroll the content hidden by the keyboard. I know how to reposition the content. What I need is a way to have different behavior on the same notifications types that in my are UIKeyboardWill(Show/Hide)Notification.
What I've done so far :
(1) I've added this code in each controller


    [[NSNotificationCenter defaultCenter] addObserver:self
                                  selector:@selector(keyboardDidAppear:)
                                      name:UIKeyboardWillShowNotification
                                    object:self.view.window
as I said I've added this code to A and B, but doesn't work as I expected. For instance When I click inside the textView two methods are triggered the keyboardDidAppear of A and the keyboardDidAppear of B, the same happens with the UIKeyboardWillHideNotification. I'm sure I'm doing something wrong, but honestly I can't figure it out.

回答1:


I solved this problem changing the place in wich I register for the notification. To make sure that only the viewController visible is the controller that receive the notification I register for the notification in vieWillAppear and remove the notification in viewWillDisappear.




回答2:


Your syntax is a bit messed too, you need to add the word selector after the @ ...

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidAppear:)
                                                 name:UIKeyboardWillShowNotification
                                               object:self.view.window];


来源:https://stackoverflow.com/questions/4901174/ios-nsnotificationcenter-multiple-uikeyboard-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!