将数据模型分配给appDelegate中带有故事板的视图控制器-ios
没有将数据模型分配给appDelegate中带有故事板的视图控制器-ios,ios,storyboard,uistoryboard,appdelegate,Ios,Storyboard,Uistoryboard,Appdelegate,没有情节串连板的情况下,我使用AppDelegate的didfishlaunchingwithoptions中的此代码将数据模型分配给视图控制器: //Data Model Class DataModel *model=[[DataModel alloc] init]; // TableViewController Controller *controller=[[Controller alloc] initWithModel:model style:UITableViewStylePlain
情节串连板
的情况下,我使用AppDelegate的didfishlaunchingwithoptions
中的此代码将数据模型
分配给视图控制器
:
//Data Model Class
DataModel *model=[[DataModel alloc] init];
// TableViewController
Controller *controller=[[Controller alloc] initWithModel:model style:UITableViewStylePlain];
self.window.rootViewController=controller;
在查看控制器的方法的initWithModel中:
-(id)initWithModel:(SBQAllReadersModel *) aModel
style:(UITableViewStyle) aStyle{
if (self=[super initWithStyle:aStyle])
{
_model=aModel;
}
return self;
}
但是有了情节提要
,我无法做到:
//Data Model Class
DataModel *model=[[DataModel alloc] init];
UIStoryboard *mainStoryboard = [UIStoryboardstoryboardWithName:@"MainStoryboard" bundle: nil];
Controller *controller = [mainStoryboard instantiateViewControllerWithIdentifier:@"Controller"];
controller.model=model;
问题是在情节提要
中,我不能使用视图控制器
的init
方法,因为它不能被情节提要
识别。我必须使用initWithCoder:
,它不能被覆盖,并且我不能将参数传递给awakeFromNib
您建议我如何从appDelegate
将带有情节提要的数据模型
分配给控制器
非常感谢您考虑到数据模型的分配方式,我认为在first view控制器中使用惰性getter(甚至比您以前的方法)更为优越。e、 g
在视图控制器的initWithCoder
:中,我分配了模型
-(id)initWithCoder:(NSCoder *)aDecoder{
if(self = [super initWithCoder:aDecoder])
{
// Do something
_model=[[DataModel alloc] init];
}
return self;
}
是的,最好用你说的懒惰的人;)
因此,我必须在rootViewControler
中分配模型
,并通过委托
将模型传递给需要使用它的下一个控制器吗 谢谢!!请看我的答案。
-(id)initWithCoder:(NSCoder *)aDecoder{
if(self = [super initWithCoder:aDecoder])
{
// Do something
_model=[[DataModel alloc] init];
}
return self;
}