簡體   English   中英

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

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM