![](/img/trans.png)
[英]How to create an UIViewController with UINavigationController with Interface Builder?
[英]How to custom init a UIViewController of Interface Builder with managed context
我正在Interface Builder中创建一个带有NavigationController和BandListViewController(UITableViewController)的简单接口,并将委托设置为AppDelegate属性。
@interface CRUDAppDelegate : NSObject <UIApplicationDelegate> {
UINavigationController *bandNav;
BandListViewController *bandList;
}
和
但是,我无法弄清楚我如何初始化我的BandListViewController传递paramater managedObjectContext而不在awakeFromNib上设置它。 CRUDAppDelegate已经初始化这个控制器并将自己的nib设置到navigationController中,但是当我尝试使用initInManagedObjectContext在didFinishLaunchingWithOptions中创建一个新的BandListViewController时,显示(TableViewController)仍然是旧的bandList。 (使用managedObjectContext = null)
到目前为止我所做的是将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;
}
我想要的
- (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;
}
恭喜; 你发现了Interface Builder的许多恼人限制之一!
在代码中完成所有操作并避免头痛。 真的没有那么多代码。
或者,从当前状态开始的更简单方法是在nib中实例化“虚拟”控制器,然后执行类似bandNav.viewControllers = [NSArray arrayWithObject:bandList];
。
在application:didFinishLaunchingWithOptions:
您正在创建BandListViewController
的新实例,但您永远不会将其视图插入到视图层次结构中,因此它永远不会显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.