开发者

Best practice for UIKeyboard notifications - iPhone SDK

开发者 https://www.devze.com 2023-01-07 23:34 出处:网络
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 foreach.My question is where to \"

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消