2012-12-09 75 views
0

Possible Duplicate:
UIAlertView crashes when added to app第二屏幕中的標籤欄控制器

我是Objective-C和Xcode的新手。在我的應用程序中,第一個屏幕是一個視圖控制器和一個按鈕。當我按下該按鈕時,我想顯示標籤欄,因此標籤欄將成爲第二個屏幕。

這裏就是我想要做的事:

appdelegate.h

@property (strong, nonatomic) FirstViewController *viewController1; 

appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.viewController1 = [[FirstViewController alloc] 
    initWithNibName:@"FirstViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController1; 
    [self.window makeKeyAndVisible]; 
    return YES; } 

在我FirsViewController.m有事件按鈕點擊:

-(IBAction) showMainView:(id) sender{ 
    ViewController *mainViewController = [[ViewController alloc] initWithNibName:@"ViewController" 
                      bundle:nil]; 
    [self.view addSubview:mainViewController.view]; 
    mainViewController = [[ViewController alloc] initWithNibName:@"ViewController"       
                  bundle:nil]; 
    [self.view addSubview:mainViewController.view]; 

ViewController是一個帶有標籤欄的xib。

在IB的第二個xib文件中,我爲「文件所有者」和AppDelegate - appdelegate類選擇了UIApplication類。然後我加入了文件所有者與應用程序代理。

當我運行我的應用程序,並單擊按鈕上我的第一個觀點,應用程序崩潰,出現以下錯誤: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x744f0f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key delegate.'

你能告訴我怎樣才能解決這個問題?可能是單擊按鈕後,我應該重新分配tabbarController爲rootViewController? 謝謝。

+0

你是否創建'mainViewController'兩次?你的代碼看起來像那樣。 – iDev

回答

0

錯誤消息看起來像,你已經在xib中設置了一個名爲delegate的東西,它沒有正確連接到類文件(ViewController)。檢查你的xib是否顯示了一些警告信息,並刪除或解決該問題。

+0

我的項目中根本沒有任何警告。如果我刪除文件的所有者(UIApplication)和AppDelegate之間的鏈接,我的應用程序崩潰與另一個異常:「[UIViewController _loadViewFromNibNamed:bundle:]加載了」ViewController「筆尖,但沒有設置視圖插座'」 – BIB

+0

@BIB,沒有在您的xib中沒有定義viewcontroller類的'delegate'? – iDev