如何在导航中使用标签栏?

时间:2012-03-28 12:37:16

标签: iphone ios5 xcode4.3

我是iphone开发的新手。目前我正在使用TABBED APPLICATION TEMPLATE做一个项目,其中包含4个选项卡,其中一个在表视图中有一个项目列表,点击表格单元格我想给出一个说明页面。我知道导航必须使用,我成功地引出了导航但现在的问题是我想在详细页面中也有标签栏。但是我的它不会来。 现在我正在使用此代码来进行导航

在appdelegate的didfinishlaunching中

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *viewController1 = [[[cardsAvailable1 alloc] initWithNibName:@"cardsAvailable1" bundle:nil] autorelease];
UIViewController *viewController2 = [[[fetchcard1 alloc] initWithNibName:@"fetchcard1" bundle:nil] autorelease];
UIViewController *viewController3 = [[[registration alloc] initWithNibName:@"registration" bundle:nil] autorelease];
UIViewController *viewController4 = [[[logintab alloc] initWithNibName:@"logintab" bundle:nil] autorelease];


self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3,viewController4, nil];
self.tabBarController.selectedIndex = 3;

navigationController = [[UINavigationController alloc]
                        initWithRootViewController:self.tabBarController];
self.window.rootViewController = self.tabBarController;
[self.window addSubview:navigationController.view];

[self.window makeKeyAndVisible];

[self.navigationController pushViewController:self.detailViewExample animated:YES];

以及选择导航

这样做的正确方法是什么? 任何人都可以建议我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

您的代码或解释中存在错误。您没有构建选项卡式应用程序,因为窗口中的第一个视图是NavigationController。你的代码中有一个危险的东西,因为windows上的第一个视图是navigationcontroller.view,但是rootViewController是tabViewController。不太聪明。

您要做的是每个标签中带有导航控制器的选项卡式应用程序:

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] 
                                                    bounds]] autorelease];

UIViewController *viewController1 = [[[cardsAvailable1 alloc] 
                           initWithNibName:@"cardsAvailable1" bundle:nil] autorelease];
UIViewController *viewController2 = [[[fetchcard1 alloc] 
                                initWithNibName:@"fetchcard1" bundle:nil] autorelease];
UIViewController *viewController3 = [[[registration alloc] 
                              initWithNibName:@"registration" bundle:nil] autorelease];
UIViewController *viewController4 = [[[logintab alloc] 
                                  initWithNibName:@"logintab" bundle:nil] autorelease];

self.tabBarController = [[[UITabBarController alloc] init] autorelease];

self.tabBarController.viewControllers = [NSArray arrayWithObjects:
    [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease], 
    [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease],
    [[[UINavigationController alloc] initWithRootViewController:viewController3] autorelease],
    [[[UINavigationController alloc] initWithRootViewController:viewController4] autorelease], 
nil];

self.tabBarController.selectedIndex = 3;

self.window.rootViewController = self.tabBarController;
[self.window addSubview:tabBarController.view];

[self.window makeKeyAndVisible];

UINavigationController * navController = [[tabBarController viewControllers] objectAtIndex:3];
[navController pushViewController:self.detailViewExample animated:YES];

答案 1 :(得分:0)

好吧我做的是我有大约5个标签,每个都有导航控制器

你可以保管下面的代码

//Initialize all the object of classes for Tabs and Navigation
Recipes *frstObj= [[Recipes alloc]init];
GalaryView *galObj = [[GalaryView alloc] init];
FavPageView *favObj = [[FavPageView alloc]init];
MyRecipes *addRec = [[MyRecipes alloc]init];
SearchSelection *searchTab = [[SearchSelection alloc] init];
//EnterContactsViewController *addRec = [[EnterContactsViewController alloc]initWithNibName:@"EnterContactsViewController_iPhone" bundle:nil];

[self.window addSubview:frstObj.view];


navContobj1 = [[UINavigationController alloc] init];
navContobj2 = [[UINavigationController alloc] init];
navContobj3 = [[UINavigationController alloc]init];
navContobj4 = [[UINavigationController alloc]init];
navContobj5 = [[UINavigationController alloc]init];

[navContobj1 pushViewController:frstObj animated:YES];
[navContobj2 pushViewController:galObj animated:YES];
[navContobj3 pushViewController:favObj animated:YES];
[navContobj4 pushViewController:searchTab animated:YES];
[navContobj5 pushViewController:addRec animated:YES];
[frstObj release];
[galObj release];
[favObj release];
[searchTab release];
[addRec release];

//Set Title
navContobj2.title = @"Galary";
navContobj3.title = @"Favoruite";
addRec.title = @"Add Recipe";
navContobj4.title = @"Search Recipes";


UITabBarController *tab = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
[tab setViewControllers:[NSArray arrayWithObjects:navContobj1,navContobj2,navContobj3,navContobj4,navContobj5,nil]];




// use extended method to set background color
//[tab setBackground];
 [self copyDatabaseIfNeeded];
// Override point for customization after application launch.
[self.window addSubview:viewController.view];
[self.window addSubview:tab.view];
[self.window makeKeyAndVisible];