【问题标题】:UITabBar and more than 1 UINavigationControllerUITabBar 和 1 个以上的 UINavigationController
【发布时间】:2010-11-12 16:46:43
【问题描述】:

我目前正在开发一个需要我有不同 UINavigationControllers 的应用程序 - 所以我正在使用标签栏并 尝试 使用 UITabBar 在它们之间进行交换,所以我在应用程序中像这样委托一些代码:

// Setting up the views for the tab controller
Friends *friends = [[[Friends alloc] initWithNibName:@"Friends" bundle:[NSBundle mainBundle]] autorelease];
WifiManager *wifi = [[[WifiManager alloc] initWithNibName:@"WifiManager" bundle:[NSBundle mainBundle]] autorelease];

UINavigationController *locationController = [[UINavigationController alloc] initWithRootViewController:wifi];      
UINavigationController *friendsController = [[UINavigationController alloc] initWithRootViewController:friends];

//Set up the tab controller
tabBarController = [[UITabBarController alloc] init];

tabBarController.viewControllers =
[NSArray arrayWithObjects:locationController, friendsController, nil];

//Add the tab bar to the window
[window addSubview:tabBarController.view];

这将编译并加载第一个 UINavigationController 但是当我单击另一个导航控制器时,我得到:

*** -[NSCFData tabBarItem]: unrecognized selector sent to instance 0x1152b0'

最奇怪的部分是我可以将选项卡控制器与单个 UINavigationController 一起使用,并且一切正常,但是当我尝试添加第二个时,它惨遭失败 - 有没有人知道我做错了什么在这里?

提前谢谢你

詹姆斯

【问题讨论】:

    标签: iphone uinavigationcontroller uitabbarcontroller


    【解决方案1】:

    我有一个以类似方式工作的应用程序。我处理这个的方法是把它抽象一点。也就是说,让顶层(默认窗口和东西下面)只是标签栏控制器。我建议在这里派生一个自定义类,以便您可以获取代码。然后让每个导航栏控制器驻留在该选项卡栏内(在 nib 结构内),但只担心在显示时添加它们。

    使用 Interface Builder:使用 IB 非常简单。在我的 MainWindow.xib 文件中,顶层包含所有正常的东西,窗口、通用 UITabBarController 和我希望推送到每个 UINavigationController 上的 UIViewController,我们会说 UIViewController 1b 和 2b(1a 和 2a 是两个 UIViewController是每个导航栏的默认视图)。嵌套在 UITabBarController 中,我有 UITabBar 和我的两个 UINavigationControllers。在每一个中,我依次有一个 UINavigationBar、一个 UIViewController 和一个 UITabBarItem。这是我的代码在应用程序委托中的样子:

    [window addSubview:tabBarController.view];
    [tabBarController setSelectedIndex:0];
    

    然后,当我想使用导航栏时,我会这样做:

    UIViewController* newView = [[UIViewController alloc]initWithNibName:@"newViewController" bundle:nil];
    [self.navigationController pushViewController:newView animated:TRUE];
    [newView release];
    

    这就是我让它工作所需的全部内容(我可能忘记了一些 IB 接线)。

    我最后的想法是这个包可能会弄乱你的导航栏。我从来没有使用过它,也不太了解它的优缺点,但你可以尝试杀死它,看看它是否是一个修复,至少是暂时的。

    【讨论】:

      【解决方案2】:

      您是否验证过每个单一视图控制器(Friends 和 WifiManager)在只有一个视图控制器时都能正常工作?可能你的问题不是“两个控制器”,而是“一个控制器坏了”。

      【讨论】:

      • 我刚刚在我自己的产品上试验了 Kendall 关于不自动发布的建议。我认为这不是你的问题,尽管它确实很容易测试。
      • 我确实有点昏暗 - 我忘记包含 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 方法,似乎使用 UINavigationController 这是必需的方法,你我以为编译器会在看到它不存在时抛出警告......
      • 我曾经和一个很棒的 C 程序员一起工作,他对着编译器喊了几个小时,因为他标记了他完全普通的“for循环”: if (i = 0; i
      • P.S. -- 关于忘记 numberOfSectionsInTableView 方法的问题:如果你使用 UITableViewController 模板创建表格类,它会自动出现。他们改变了您选择模板的方式,但它就在那里。 (选择 NSObject,然后从“选择子类”控件中选择 UITableViewController。)
      【解决方案3】:

      该代码应该可以工作。

      我能想到的唯一建议就是不要自动释放您正在创建的视图控制器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-21
        • 1970-01-01
        • 1970-01-01
        • 2015-02-14
        • 1970-01-01
        • 2017-08-22
        • 2015-01-11
        相关资源
        最近更新 更多