Elton's Blog

Tag: UINavigationController

UITableView reloadData或viewWillAppear失效

by Elton on 2009年11月24日, under iPhone

今天在做开发的时候,忽然发现在视图的viewWillAppear:方法中添加:

  1.  
  2. [self.tableView reloadData];
  3.  

不起作用,viewWillAppear:这个方法根本没有调用

后来发现原来用了UINavigationController后,viewWillAppear方法是没有效果的,要用UINavigationControllerDelegate的– navigationController:willShowViewController:animated:方法才可以达到这个目的。

所以要做到这个,你必须做以下几步:

1. 设置代理类

  1.  
  2. nav.delegate = self;
  3.  

2. 代理类实现UINavigationControllerDelegate Protocol
3. 在代理类中添加– navigationController:willShowViewController:animated:方法

如:

  1.  
  2. - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  3.         [self.myTableView reloadData];
  4. }
  5.  
Leave a Comment :, , , , more...

loadView时候的注意事项

by Elton on 2009年11月17日, under iPhone

loadView是使用代码生成视图的时候,当视图第一次载入的时候调用的方法。

今天写一个Navigation Controller,给它加一个继承了TableViewController的View作为Navigation Controller的Root View

  1.  
  2. UITableViewController *tableViewController = [[TableViewController alloc] init];
  3.  
  4. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: tableViewController];
  5. nav.navigationBar.barStyle = UIBarStyleBlack;
  6. [self.view addSubview:nav.view];
  7.  

一切都很顺利成章,但是运行的时候,除了显示一个NavigationBar以外,表格并没有显示出来。 反复查找原因都为果。 后来仔细对比之前的代码,原来是tablebView中的loadView方法少写了下面这行代码:

  1.  
  2. [super loadView];
  3.  

后来查了文档,说的很清楚,自己的类里面的loadView一定要第一行加上[super loadView]这句话。 看来以后还是要多注意细节。

1 Comment :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...