Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct time to call viewDidAppear manually?

I have a UITableViewController in my app, which is added to the view hierarchy directly. After the view appears, I want to scroll to a specific cell. My solution would be to call the code for scrolling in -[viewDidAppear].

According to Apple's docs I have to call the method manually:

If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly.

The question is: When is the right time to call it manually?

Calling it from the parent view controller's -[viewDidAppear] leads to a crash when I try to do the scrolling because apparently, the table view actually didn't yet appear and therefore thinks it has no sections to scroll to.

like image 600
mrueg Avatar asked Sep 22 '09 17:09

mrueg


People also ask

Can I call viewwillappear?

Well, you certainly may call them but Apple says don't. Misusing any method by disregarding Apple's official documentation for the class methods is asking for trouble. If you think you need to send those messages, you're doing something wrong somewhere.

What is called after viewDidAppear?

viewDidAppear is called once you see the loaded view on screen. It is called after view appeared. ViewDidAppear is called everytime when you see the view after it is loaded. if you push and then pop any other viewController on that view then again viewDidAppear gets called.

Which is called first viewDidLoad or viewDidAppear?

viewDidLoad is called once when the controller is created and viewDidAppear is called each time the view, well, DID appear. So say you have a modal view that you present, when that view is dismissed, viewDidAppear will be called, and viewDidLoad will not be called.


2 Answers

If you are using view controller containment don't call viewWillAppear: directly. Instead use – beginAppearanceTransition:animated: and – endAppearanceTransition.

If you are implementing a custom container controller, use this method to tell the child that its views are about to appear or disappear. Do not invoke viewWillAppear:, viewWillDisappear:, viewDidAppear:, or viewDidDisappear: directly.

Calling addSubView will automatically trigger viewWillAppear: and viewDidAppear: if the view's viewController is a child view controller, therefore calling viewWillAppear: directly will trigger view will appearance method twice. Using beginAppearanceTransition:animated:and– endAppearanceTransition` will suppress the automatic behaviour so you only get it called once.

like image 161
Robert Avatar answered Oct 05 '22 20:10

Robert


In -[viewDidAppear] on the tableview, called indeed from the parent view controller's -[viewDidAppear], you can call [tableView reloadData], this way you ensure that the tableView is loaded and ready.

like image 33
luvieere Avatar answered Oct 05 '22 21:10

luvieere