简体   繁体   English

如何使用托管上下文自定义Interface Builder的UIViewController

[英]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.

相关问题 如何使用Interface Builder创建带有UINavigationController的UIViewController? - How to create an UIViewController with UINavigationController with Interface Builder? 连接到我的自定义UIViewController的Interface Builder中的UIView构建 - UIView build in Interface Builder connected to my custom UIViewController UIVIewController自定义init方法 - UIVIewController custom init method 如何在从界面构建器中的Storyboard场景实例化的视图控制器上调用自定义init? - How do I call a custom init on a view controller instantiated from a Storyboard scene in interface builder? 在另一个的UIViewController初始化中初始化自定义UIViewController - Initialize Custom UIViewController in another's UIViewController init 如何使用UIInterfaceOrientationLandscapeLeft初始化UIViewController? - How to init UIViewController with UIInterfaceOrientationLandscapeLeft? UIViewController占据了Interface Builder中的整个屏幕 - UIViewController takes up entire screen in Interface Builder 如何在没有Interface Builder的UIViewController中实现UISearchDisplayController - Howto implement UISearchDisplayController in a UIViewController without Interface Builder 在Interface Builder中更改UINavigationController UIViewController的标题视图 - Changing the Title View of a UINavigationController UIViewController in Interface Builder 接口构建器中的设计视图可以延迟加载到UIViewController中 - Design view in Interface Builder to load lazily in UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM