[英]How to custom init a UIViewController of Interface Builder with managed context
I'm creating a simple interface with NavigationController and BandListViewController(UITableViewController) inside Interface Builder and setting the delegation to AppDelegate properties. 我正在Interface Builder中创建一个带有NavigationController和BandListViewController(UITableViewController)的简单接口,并将委托设置为AppDelegate属性。
@interface CRUDAppDelegate : NSObject <UIApplicationDelegate> {
UINavigationController *bandNav;
BandListViewController *bandList;
}
and 和
However, I can't figure out how can I initialize my BandListViewController passing the paramater managedObjectContext without setting it on awakeFromNib. 但是,我无法弄清楚我如何初始化我的BandListViewController传递paramater managedObjectContext而不在awakeFromNib上设置它。 The CRUDAppDelegate already init this controller and set his own nib into the navigationController, but then when I try to make a new BandListViewController in didFinishLaunchingWithOptions with initInManagedObjectContext, the display (TableViewController) remains of the old bandList. CRUDAppDelegate已经初始化这个控制器并将自己的nib设置到navigationController中,但是当我尝试使用initInManagedObjectContext在didFinishLaunchingWithOptions中创建一个新的BandListViewController时,显示(TableViewController)仍然是旧的bandList。 (with managedObjectContext = null) (使用managedObjectContext = null)
What I've done so far is keeping the bandList managedObjectContext at awakeFromNib as Apple suggests. 到目前为止我所做的是将bandList managedObjectContext保持在awakeFromNib,正如Apple建议的那样。
- (void)awakeFromNib
{
/*
Typically you should set up the Core Data stack here, usually by passing the managed object context to the first view controller.
self.<#View controller#>.managedObjectContext = self.managedObjectContext;
*/
self.bandList.managedObjectContext = self.managedObjectContext;
}
What I wanted 我想要的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.bandList = [[BandListViewController alloc] initInManagedObjectContext:self.managedObjectContext];
// Override point for customization after application launch.
[self.window addSubview:bandNav.view];
[self.window makeKeyAndVisible];
return YES;
}
Congratulations; 恭喜; you've found one of the many annoying limitations of Interface Builder! 你发现了Interface Builder的许多恼人限制之一!
Do it all in code and save yourself the headache. 在代码中完成所有操作并避免头痛。 There really isn't that much code. 真的没有那么多代码。
Alternatively, the easier way from your current state is to instantiate a "dummy" controller in the nib and then do something like bandNav.viewControllers = [NSArray arrayWithObject:bandList];
或者,从当前状态开始的更简单方法是在nib中实例化“虚拟”控制器,然后执行类似bandNav.viewControllers = [NSArray arrayWithObject:bandList];
. 。
在application:didFinishLaunchingWithOptions:
您正在创建BandListViewController
的新实例,但您永远不会将其视图插入到视图层次结构中,因此它永远不会显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.