开发者

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

How to custom init a UIViewController of Interface Builder with managed context

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 Co开发者_Go百科re 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];.


In application:didFinishLaunchingWithOptions: you are creating a new instance of BandListViewController but you are never inserting its view into the view hierarchy so it never gets displayed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜