首页 > uitabbarcontroller > UITabBarController中的UIButton在单击时崩溃!

UITabBarController中的UIButton在单击时崩溃! (UIButton in a UITabBarController crashing when clicked!)

问题

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

因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' -  [NSCFString playButton]:无法识别的选择器发送到实例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

提前致谢!

解决方法

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

真正的问题出在我的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];

问题

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!

解决方法

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

相似信息