2009-05-25 47 views
6

我有一个应用程序可以在iPhone OS 2.2.1上完美工作,但是当我尝试在iPhone OS 3.0上运行它时,它会崩溃。“更改标签栏的代理”异常

下面是我从控制台得到了错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.' 

也许这是因为我在编程改变某个视图控制器的看法。

下面是代码:

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear: animated]; 

    self.view = current_controller.view; 
    [current_controller viewWillAppear: NO]; 
    [current_controller viewDidAppear: NO]; 
} 

可发生在这部分代码错误,如果是我该如何解决? 为什么还会发生?

谢谢你, 伊利亚。

回答

4

上面的恩斯特先生给人的印象是,他看到伊利亚代码中的某些东西构成了“从控制器下取景”。这可能让你长时间盯着代码,而这不是问题的真正原因。我在Apple Developer Forum http://discussions.apple.com/message.jspa?messageID=10259835#10259835上发布了这个问题,我被告知'NSInternalInconsistencyException'是一个.xib文件的问题(在Interface Builder中)。使用这些信息我找到了以下解决方案。我想这里给出的一些名字是通用的,并且会帮助其他人试图解决这个问题。为了回顾这个问题,xib参考编译并在2.x上完美运行,在3.x上编译,并在您尝试在3.0模拟器中运行该应用程序时出现上述错误消息。我在一个标签栏中有一个代表。在界面构建器中查看引用插座时,我将“多个”,“文件所有者”,“选项卡栏”和“选项卡栏控制器”作为引用插座。当我从参考插座中删除“标签栏”时,我的应用程序在模拟器3.0中运行。它也编译并在2.x上运行,所以“标签栏”引用不是2.x所要求的。 ...飞侠哥顿

+0

这正是我做了! :) – 2009-11-17 06:23:37

0

很简单,你不能这样做。从UIViewController下取出视图是一个肯定的方式来获取崩溃。

请看Apple提供的标签栏教程,了解它是如何正确完成的。

0

这是一个不平凡的问题。有人必须发布一个从ViewController下删除视图的例子,因为很多人都这样做了。尽管所有关于它有多“糟糕”的讨论都是在2.x中运行的。即使是苹果的文章也没有涉及架构问题。大多数人会写一个.h/.m组合来处理每个子控制器视图。苹果示例似乎只在控制tabbarcontroller的.m文件中操作。

0

我发现我以下解决方案:

  1. 子类的UITabBarController
  2. 在Interface Builder中设置的“自定义类>类”您UITabBar的属性为新的类
  3. 下面的代码添加到您的类

    -(void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray *)items{ 
        id modalViewCtrl = [[[self view] subviews] objectAtIndex:1]; 
        if([modalViewCtrl isKindOfClass:NSClassFromString(@"UITabBarCustomizeView")] == YES) 
         ((UINavigationBar*)[[modalViewCtrl subviews] objectAtIndex:0]).tintColor = [UIColor blackColor]; 
    } 
    

希望这有助于...

7

在大多数情况下,您可以使用UITabBarControllerDelegate。它有类似于UITabBarDelegate的方法并避免了这种异常。因为,例如,而不是:


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { 

    int index = [tabBar determinePositionInTabBar:item]; // custom method 
    [tabBar doSomethingWithTabBar]; 
    [item doSomethingWithItem]; 
    [item doSomethingWithItemAndIndex:index]; 
} 

你可以写:


- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 

    UITabBarItem *item = [tabBarController.tabBar selectedItem]; 
    int index = [tabBarController.tabBar determinePositionInTabBar:item]; // custom method 
    [tabBarController.tabBar doSomethingWithTabBar]; 
    [item doSomethingWithItem]; 
    [item doSomethingWithItemAndIndex:index]; 
} 
+1

不错的选择(Y) – 2016-01-30 04:18:12