Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for UIKeyboard notifications - iPhone SDK

I have a drill down navigation app with three levels of UIViewControllers. In each view controller, I have a UITextField where I am trying to subclass the UIKeyboard for each. My question is where to "set" notifications and "unset" them.

I have the notifications:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

so it it best to set them in the viewDidLoad event? Or the viewWillAppear event?

And likewise for [[NSNotificationCenter defaultCenter] removeObserver:self];

I don't want to have multiple keyboardWillShow: events to be called as I drill down.

Many thanks, Brett

like image 451
Brett Avatar asked Jul 18 '10 02:07

Brett


2 Answers

I suggest you put these in the init and dealloc methods, as the viewWillAppear and viewWillDisappear will be called every time the view appears or disappears, which is unnecessary for registering/deregistering notifications.

like image 65
Jacob Relkin Avatar answered Sep 21 '22 22:09

Jacob Relkin


I'd nevertheless suggest you register as observer in viewWillAppear and unregister in viewWillDisappear since viewDidUnload is called only when memory has to be freed, meaning viewDidLoad get called much more often than viewDidUnload and then you might have the problem of getting the same notification more than once.

like image 25
drct Avatar answered Sep 19 '22 22:09

drct