views:

209

answers:

1

Hello!

A friend gave me a small tutorial to iPhone programming yesterday. I then tried to make a small program with two views which I can switch using a TabBar: One with a blue background, one with a red one.

It displays them just fine, but when I run it (in the Simulator) and click on a Tab, it crashed with :

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMachPort _tabBarItemClicked:]: unrecognized selector sent to instance 0x38223e0"

I didn't manage to find the bug so far... Have you got any ideas ? :)

Code from 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];

}

Code excerpt from 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];
}
+2  A: 

The problem seems to be in this line: [tbcontroller release];

This is deallocating your tbcontroller. I've commented out that line of code and it works. Try making sure to retain your controller somewhere else since apparently adding the subview isn't doing the job. It may also be related to the viewcontroller themselves being released.

mjdth
That certainly solves the crash, thanks.But do you have an idea why the subview does not retain it?
Patrick