开发者

viewWillAppear not called in UITableViewController?

I have a couple of UITableViewController classes and I just noticed that these methods aren't being called:

-(void)viewWillAppear:(BOOL)animat开发者_StackOverflow社区ed;
-(void)viewDidAppear:(BOOL)animated;

I read in http://discussions.apple.com/thread.jspa?threadID=1529769&tstart=0 that I would have to call those methods myself when pushing view controllers, but that's strange, since it works for anything but UITableViewController.

Also makes it a bit of an issue when I need to have a UITableViewCell deselected in the UIViewController that pushed the UITableViewController.


I can't find it in the documentation, but I think this might be because you are using a UINavigationController.

How about setting the UINavigationController's delegate property and then implementing UINavigationControllerDelegate? It provides two optional methods:

– navigationController:willShowViewController:animated: 
– navigationController:didShowViewController:animated: 

For example, navigationController:willShowViewController:animated: might look something like this:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  if ([viewController isKindOfClass:[UITableViewController class]]) {
    [viewController viewWillAppear:animated];
  }
}

Anyway, this will get you the behavior you want without having to hack calls to viewWillAppear: all over your project.


Did anyone resolve this because the original post is correct - simply using UITableViewController and pushing the table view of that controller onto the navController does NOT trigger these methods despite the fact that it should. I have a series of UITableViewControllers and table views that are pushed and popped to display hierarchical data - nothing fancy, but the "viewWill/Did/Appear/Disappear" methods are never called. Only the viewDidLoad and viewDidUnload are called.

There must be a wiring problem in both our setups, but simply pushing a view into the navigationController should be all that is required (?) - hard to believe that this could have gone unnoticed as a fundamental bug this long.

?


These two methods are called by default to notify for the changes. UITableViewController is a subclass of UIViewController, so there will be the same behavior. You can see more in the View Controller Programming Guide

The viewWillAppear: and viewDidAppear: methods give subclasses a chance to perform any additional actions related to the appearance of the view.

How do you know that these methods are not called? Can you provide some more codes, or at least you test them with a NSLog() to see if there are some messages printed.


I'm seeing the same problem. I have a simple UIView from the IB and I do a addSubview with a class that extends UITableViewController.

I can see the view of the TableViewController without problems in my application, but the viewWillAppear function is never called in this situation.


Well, the discussion linked from the question has the answer right in it. UINavigationController needs to receive the "viewWillAppear" message in order for it to send those messages to the view controllers you push onto it.

So ironically if you don't do what Apple recommends, and you subclass UINavController for your view controller, then everything works great.

However, if you just create a UINavController inside of your view controller, then you need to implement "viewWillAppear", "viewDidAppear" and so on and forward those to your nav controller.

Note that this is especially important if you're using Three20, because its view controller hierarchy expects the "viewWillAppear" message to be received. If its not you can end up with TTTableViews that don't draw.


The same can occur if you use a UITabViewController. You need to force the viewWillAppear call by implementing either the UITabViewControllerDelegate or UINavigationControllerDelegate callbacks


This explanation may help: http://www.mlsite.net/blog/?p=210

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜