2009-12-18 23 views
0

一個朋友昨天給了我一個小教程iPhone編程。 然後我試圖做一個小程序,使用它,我可以使用的TabBar切換兩種觀點:一是與藍色背景,一個用紅色的。異常與中的TabBar iPhone SDK的

這顯示他們就好了,但是當我運行它(在模擬器),點擊一個標籤,它與崩潰:

「終止應用程序由於未捕獲 例外 ‘NSInvalidArgumentException’的,原因: 「*** - [NSMachPort _tabBarItemClicked:]:無法識別的選擇發送到實例0x38223e0"

我沒能找到的bug那麼遠,你有什麼想法? :)

從DemoAppDelegate代碼:

- (void)applicationDidFinishLaunching:(UIApplication *)application {  

    // Override point for customization after application launch 
    [window makeKeyAndVisible]; 


    UIColor *color = [UIColor blueColor]; 
    DemoAppViewController *controller1 = [[DemoAppViewController alloc] initWithColor:color]; 

    color = [UIColor redColor]; 
    DemoAppViewController *controller2 = [[DemoAppViewController alloc] initWithColor:color]; 


    [controller1 updateBackground]; 
    [controller2 updateBackground]; 


    UITabBarController *tbcontroller = [[UITabBarController alloc] init]; 
    NSMutableArray *array = [NSMutableArray arrayWithObjects: controller1, controller2, nil]; 

    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Blue" image:nil tag:1]; 
    [controller1 setTabBarItem: item]; 
    [item release]; 

    item = [[UITabBarItem alloc] initWithTitle:@"Red" image:nil tag:2]; 
    [controller2 setTabBarItem: item]; 
    [item release]; 

    [tbcontroller setViewControllers:array]; 
    [tbcontroller setSelectedIndex:0]; 

    [window addSubview: [tbcontroller view]]; 
    [tbcontroller release]; 

    [controller1 release]; 
    [controller2 release]; 

} 

代碼摘自DemoAppViewController:

- (DemoAppViewController *) initWithColor:(UIColor *)color 
{ 
    self = [super init]; 

    if (self != nil) { 
     [color retain]; 
     backgroundColor = color; 
    } 

    return self; 
} 

- (void) updateBackground 
{ 
    UIView *view = [self view]; 
    [view setBackgroundColor: backgroundColor]; 
} 

回答

3

的問題似乎是在這一行:[tbcontroller release];

這是重新分配你的tbcontroller。我已經註釋到了這一行代碼,它的工作原理。請嘗試確保將控制器保留在其他地方,因爲顯然添加子視圖不會完成這項工作。它也可能與視圖控制器本身被釋放有關。

+0

這無疑解決了死機,謝謝。 但你有一個想法,爲什麼子視圖不保留它? – Patrick