viewWillAppear vs Viewdidload ios

2019-04-24 12:38发布

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.

2条回答
在下西门庆
2楼-- · 2019-04-24 12:59

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
}
查看更多
干净又极端
3楼-- · 2019-04-24 13:10

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];
查看更多
登录 后发表回答