2009-07-21 79 views
0

我目前正在研究一个应用程序,需要我有不同的UINavigationControllers - 所以我使用标签栏和企图使用UITabBar在它们之间进行交换,所以我有应用程序委托一些代码,像这样:UITabBar和超过1 UINavigationController

// 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的标签控制器,一切正常,但是当我尝试添加第二个时,它失败了 - 有没有人有任何想法,我在这里做错了?

预先感谢您

詹姆斯

回答

3

您是否验证了每个单视图控制器(Friends和WifiManager)在只有一个时有效?这可能是因为你的问题不是“两个控制器”,而是“一个控制器坏了。”

+0

我刚刚在我自己的产品上试验过Kendall关于不自动释放的建议。我不认为这是你的问题,尽管它很容易测试。 – Amagrammer 2009-07-21 22:35:59

+0

我确实有点朦胧 - 我忘记了包含 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView方法,它似乎与UINavigationController这是一个必需的方法,你会想到编译器会当它看到它不在那里时发出警告...... – 2009-07-22 08:10:23

+0

我曾经与一位出色的C程序员一起工作,他在编译器上大喊几个小时来标记他完美的普通“for循环”: if(i = 0; i <5; ++ i){} 我们都昏暗偶尔。 – Amagrammer 2009-07-22 11:51:53

0

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

使用Interface Builder:使用IB很简单。在我的MainWindow.xib文件中,顶层包含所有普通的东西,Window,通用的UITabBarController和UIViewControllers,我希望将它们推送到每个UINavigationController,我们会说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布线)。

我最后的想法是,捆绑可能会搞乱你的navbars。我从来没有使用过它,对利弊也不太了解,但是你可能会试图杀死它,看看它是否是一种修复,至少是暂时的。

0

该代码应该工作。

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