简体   繁体   中英

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 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. 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. (with managedObjectContext = null)

What I've done so far is keeping the bandList managedObjectContext at awakeFromNib as Apple suggests.

- (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!

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]; .

application:didFinishLaunchingWithOptions:您正在创建BandListViewController的新实例,但您永远不会将其视图插入到视图层次结构中,因此它永远不会显示。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM