iPhone SDK 中的 TabBar 异常
昨天一位朋友给了我一个 iPhone 编程的小教程。 然后,我尝试制作一个具有两个视图的小程序,我可以使用 TabBar 进行切换:一个具有蓝色背景,一个具有红色背景。
它显示它们很好,但是当我运行它(在模拟器中)并单击选项卡时,它崩溃了:
“由于未捕获而终止应用程序 例外 'NSInvalidArgumentException',原因: '*** -[NSMachPort _tabBarItemClicked:]: 无法识别的选择器发送到实例 0x38223e0"
到目前为止我还没有找到该错误...您有什么想法吗?:)
来自 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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎出在这一行:
[tbcontroller release];
这是解除分配您的 tbcontroller。我已经注释掉了那行代码并且它有效。尝试确保将控制器保留在其他地方,因为显然添加子视图并不能完成这项工作。也可能与viewcontroller本身被释放有关。
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.