UITabBarController中的UIButton在单击时崩溃!

时间:2010-08-23 09:07:41

标签: uitabbarcontroller uibutton click

我试图以编程方式在UIView中包含一个自定义按钮,这是UITabBarController的子视图。 按钮显示正常,但当我单击它时,它崩溃没有错误消息。奇怪的是,有时它不一致:

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

我甚至尝试删除播放按钮方法中的任何代码,我尝试将名称playButton更改为playAction,我也尝试将按钮直接添加到“self”而不是aboutView但结果仍然相同。

我的猜测是它与tabBar有关,它有一个UIView作为子视图,上面有一个按钮。我不知道。

以下是我在appDelegate方法中构建tabBar的代码片段



// 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];

并且是实现AboutViewController类的代码

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

提前致谢!

2 个答案:

答案 0 :(得分:0)

[playSoundButton = [UIButton buttonWithType:UIButtonTypeCustom] retain];应该阅读playSoundButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

(即将第一个[再向右移动到UIButton

答案 1 :(得分:0)

感谢反馈,我实际上知道如何实现保留声明,这是一个我没注意到的拼写错误。令人惊讶的是,这不是问题,代码运行良好,现在让错字保持不变。

真正的问题出在我的appDelegate方法中。 代替

    // 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];
应该
    // 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];