views:

15

answers:

2

I tried to include a custom button programmatically inside a UIView which is a subview of UITabBarController. The button is displays fine but when I click it, it crashes without an error message. Its strange that sometimes it does inconsistently:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString playButton]: unrecognized selector sent to instance 0x632cd10'

I even tried removing any code in my play button method, i tried changing the name playButton to playAction, I also tried add the button directly to "self" not to aboutView but the result is still the same.

My guess is it got something to do with the tabBar having a UIView as a subview with a button on it. I don't know.

Here's a code snippet of how i built the tabBar in my appDelegate method



// About Tab
 aboutViewC = [[[AboutViewController alloc] init] autorelease];
 aboutNavC = [[[UIViewController alloc] init] autorelease];
 aboutNavC.title = @"About";
 aboutNavC.view = aboutViewC.view;

 // Lessons Tab
 lessonsViewC = [[[LevelViewController alloc] init] autorelease];
 lessonsViewC.title = @"Levels";
 lessonsNavC = [[[UINavigationController alloc] initWithRootViewController:lessonsViewC] autorelease];
 lessonsNavC.title = @"Lessons";
 lessonsNavC.viewControllers = [NSArray arrayWithObjects:lessonsViewC, nil];

 tabBarController = [[UITabBarController alloc] init];
 tabBarController.viewControllers = [NSArray arrayWithObjects:aboutNavC, lessonsNavC, nil];

and heres the code for implementing the AboutViewController class

AboutViewController.h



#import 
@interface AboutViewController : UIViewController {
 UIButton *playSoundButton;
 UIView *aboutView;
}
- (void)playButton;
@end

AboutViewController.m


#import "AboutViewController.h"

@implementation AboutViewController

- (void)dealloc {
    [playSoundButton release];
    [aboutView release];
    [super dealloc];
}

- (void)viewDidLoad {
 aboutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

 [playSoundButton = [UIButton buttonWithType:UIButtonTypeCustom] retain];
 image = [UIImage imageNamed:@"bt_play.png"];
 [playSoundButton setImage:image forState:UIControlStateNormal];
 [playSoundButton addTarget:self action:@selector(playButton) forControlEvents:UIControlEventTouchUpInside];
 playSoundButton.frame = CGRectMake(0, 350, 40, 40);
 [aboutView addSubview:playSoundButton];

 stopSoundButton.hidden = YES;
 playSoundButton.hidden = NO;

 [self.view addSubview:aboutView];

 [super viewDidLoad];
}

- (void)playButton
{
 NSLog(@"playAction method");
}
@end

Thanks in advance!

A: 

[playSoundButton = [UIButton buttonWithType:UIButtonTypeCustom] retain]; should read playSoundButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

(i.e. move the first [ further right to UIButton)

tob
A: 

Thanks tob for the feedback, i actually know how to implement retain statement, it was a typo i didn't notice. surprisingly, that is not the problem and the code run's fine now leaving the typo unchanged.

The real problem was in my appDelegate method. Instead of

    // About Tab
    aboutViewC = [[[AboutViewController alloc] init] autorelease];
    aboutNavC = [[[UIViewController alloc] init] autorelease];
    aboutNavC.title = @"About";
    aboutNavC.view = aboutViewC.view;
    // Lessons Tab
    lessonsViewC = [[[LevelViewController alloc] init] autorelease];
    lessonsViewC.title = @"Levels";
    lessonsNavC = [[[UINavigationController alloc] initWithRootViewController:lessonsViewC] autorelease];
    lessonsNavC.title = @"Lessons";
    lessonsNavC.viewControllers = [NSArray arrayWithObjects:lessonsViewC, nil];
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:aboutNavC, lessonsNavC, nil];
should be
    // About Tab
    aboutViewC = [[[AboutViewController alloc] init] autorelease];
    aboutViewC.title = @"About";
    // Lessons Tab
    lessonsViewC = [[[LevelViewController alloc] init] autorelease];
    lessonsViewC.title = @"Levels";
    lessonsNavC = [[[UINavigationController alloc] initWithRootViewController:lessonsViewC] autorelease];
    lessonsNavC.title = @"Lessons";
    lessonsNavC.viewControllers = [NSArray arrayWithObjects:lessonsViewC, nil];
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:aboutViewC, lessonsNavC, nil];

aimacworx