Let's say I've got a handful of view controllers, each of which has some textviews/textfields/webviews. There's one main view controller (MainViewController), and the rest are displayed using presentModalViewController:animated:
.
EDIT: I want to clarify that I'm not using UINavigationController — the main view controller is just a normal UIViewController, and others are displayed as modal view controllers from time to time.
I wonder how I should implement the keyboard notification subscription so that the subviews within the top most view controller can be resized correctly. I've tried 2 approaches:
1/ Subscribe to UIKeyboardWill(开发者_运维问答Show|Hide)Notification in viewDidLoad
, and unsubscribe in dealloc
.
Problem: Multiple view controllers get notified at the same time, resulting in weird layout resizing.
2/ Subscribe in viewWillAppear
, subscribe in viewDidDisappear
Problem: Sometimes viewWillAppear
isn't triggered, so I got to manually resubscribe to keyboard notifications.
Any advices are welcome.
I think , Subscribe in viewWillAppear and Unsubscribe in viewDidDisappear is more preferable then Subscribe to UIKeyboardWill(Show|Hide)Notification in viewDidLoad, and unsubscribe in dealloc.
As you mentioned there is only one issue with approach 2.
Problem: Sometimes viewWillAppear isn't triggered, so I got to manually resubscribe to keyboard notifications.
Here is the code to make sure your viewWillAppear
trigger always.
http://www.idev101.com/code/User_Interface/UINavigationController/viewWillAppear.html
Here is a thought, use visibleViewController property of UINavigationController class.
Implement the Notification in the rootViewControllers viewDidLoad method, and call a method like updateVisibleViewController where you access your most visible view controller with a call to visibleViewController
-(void)updateVisibleViewController{
[[rootNavigationController visibleViewController] "trigger your method here"];
}
精彩评论