viewWillAppear vs Viewdidload ios

血红的双手。 提交于 2019-12-22 04:51:12

问题


When code with iOS navigation application, I have facing with trouble this:

where can I put the method "initdata" for UITableView? in viewWillAppear or viewDidLoad?

please help me out.


回答1:


You can put initData as per your requirement of the app,

if your table needs to load data every time with new Data then it should be under

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //initData
}

Otherwise, if the table needs to be reload by a single Data which doesn't vary or there is not any editing operation performed on Data , you should use

- (void)viewDidLoad {
    [super viewDidLoad];
   //initData
}



回答2:


It is better to call that in initWithNibName:bundle: or initWithCoder: method and release the loaded data in -(void)dealloc.

Also, you can have that in viewDidLoad and release the loaded data in viewDidUnload. But it is better to avoid calling that from viewWillAppear:

Edit:

I hope that array is depending on the selection in the parent view. In that case, write a setter method, which sets the condition and init the data before pushing the view controller.

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
// Pass the selected object to the new view controller and depend on that, load the data.
[detailViewController initData:(id)[_list objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];


来源:https://stackoverflow.com/questions/8802356/viewwillappear-vs-viewdidload-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!