设置ViewController 数据源无法改变view
病情描述:
-
viewController创建的时候勾选了xib,然后在显示的时候调用了如下语句:
MTDetailDealViewController *detailController = [[MTDetailDealViewController alloc]init]; detailController.deal = [self.deals objectAtIndex:indexPath.item]; [self presentViewController:detailController animated:YES completion:nil];
-
发现界面根本没有按照数据源发生改变
病情原因:
-
将viewController的几个生命周期关键方法进行打印,发现: 2016-04-20 01:26:44.369 MTHD[6384:380078] -[MTDetailDealViewController initWithNibName:bundle:](114): 2016-04-20 01:26:44.370 MTHD[6384:380078] -[MTDetailDealViewController init](99): 2016-04-20 01:26:44.370 MTHD[6384:380078] -[MTDetailDealViewController setDeal:](50): 2016-04-20 01:26:45.421 MTHD[6384:380078] -[MTDetailDealViewController loadView](95):
- 联想起上篇知识总结
iOS viewController 和 view 的创建消失生命周期总结的第4点
改正如下:
MTDetailDealViewController *detailController = [[MTDetailDealViewController alloc]init];
//必须得先调用view,否则下面的句子将会无效
[detailController loadView];
detailController.deal = [self.deals objectAtIndex:indexPath.item];
[self presentViewController:detailController animated:YES completion:nil];
再次打印发现:
2016-04-20 01:29:28.728 MTHD[6452:382311] -[MTDetailDealViewController initWithNibName:bundle:](114):
2016-04-20 01:29:28.729 MTHD[6452:382311] -[MTDetailDealViewController init](99):
2016-04-20 01:29:28.983 MTHD[6452:382311] -[MTDetailDealViewController loadView](95):
2016-04-20 01:29:28.983 MTHD[6452:382311] -[MTDetailDealViewController setDeal:](50):