使用UINavigationBarController以编程方式添加UITabBarController作为现有导航控制器的第一个选项卡

时间:2011-05-04 04:19:55

标签: iphone uitabbarcontroller

我目前在我的app委托中有一个UINavigationController,我将ViewController推送到登录状态。如果登录成功,我想创建一个带导航控制器的UITabBarController作为第一个Tab,其根控制器是我正在创建的UIViewController。

我的第一个UINavigationController的RootViewController实际上充当了logincontroller的委托,因此如果用户正确登录,它会调用我的RootViewController中的方法,然后我将UITabBarController推送到堆栈中。这是我的代码:

UITabBarController *tbController = [[UITabBarController alloc] init];
    FileBrowserViewController *fileController = [[FileBrowserViewController alloc]   init];
    fileController.pathToFileDB = pathToDBUnzipped;
    fileController.parentId = @"0";

    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:fileController];

    NSMutableArray *aViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
    [aViewControllersArray addObject:navController];
    [navController release];

    [tbController setViewControllers:aViewControllersArray];

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

    [tbController release];

现在,一切正常。除了2件事。这是屏幕截图: My iPHone

1)我看不到任何uitabbar项目。如何为每个标签设置图像和文本? 2)我不想要那顶黑条。我只想要一个带有撤销按钮的杆顶部。如何删除附加栏?

4 个答案:

答案 0 :(得分:6)

当我同时拥有UINavigationController和UITabbarController时,我总是遵循这种方法: 您需要从基于视图的应用程序开始。然后在appDelegate文件中创建一个UITabbarController。

Appdelegate.h

UITabBarController *tabBarController;
// set properties

Appdelegate.m

// Synthesize

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

// Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController  
Search * search = [[Search alloc] init];  
UINavigationController *searchNav = [[UINavigationController alloc]        initWithRootViewController:search];  

Nearby* nearby = [[Nearby alloc] init];  
UINavigationController *nearbyNav = [[UINavigationController alloc] initWithRootViewController:nearby];  

Map* map = [[Map alloc] init];  
UINavigationController *mapNav = [[UINavigationController alloc] initWithRootViewController:map];  

AboutUs* aboutUs = [[AboutUs alloc] init];  
UINavigationController *aboutUsNav = [[UINavigationController alloc] initWithRootViewController:aboutUs];  

Favorites* favorites = [[Favorites alloc] init];  
UINavigationController *favoritesNav = [[UINavigationController alloc] initWithRootViewController:favorites];  

NSArray* controllers = [NSArray arrayWithObjects:searchNav,nearbyNav,mapNav,aboutUsNav,favoritesNav, nil];  
tabBarController.viewControllers = controllers; 

[window addSubview:tabBarController.view];    

您可以相应地管理您要在哪个选项卡中放置导航控制器或仅放置视图控制器。

然后在上面提到的每个视图控制器中,你需要实现

- (id)init {}

您可以在其中设置标签名称和图像。

我总是遵循这种方法,它永远不会失败。选项卡始终可见。您可以根据代码进行更改。

答案 1 :(得分:2)

隐藏上面的黑条使用 -

[self.navigationController setNavigationBarHidden:TRUE];

设置标签栏项目使用 -

表示系统项目 -

UITabBarItem *firstItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];

自定义项目 -

UITabBarItem *firstItem = [[UITabBarItem alloc] initWithTitle:@"title" image:[UIImage imageNamed:@""] tag:0];

[navController setTabBarItem:firstItem];

答案 2 :(得分:0)

这是一个关于如何组合标签栏,导航栏和/或表格视图的好视频。

http://www.youtube.com/watch?v=LBnPfAtswgw

如果您不希望注册屏幕有标签栏控制器,则必须将其显示为模态视图(因为标签栏是您的根视图控制器)。这可以通过presentModalViewController:animated:方法完成。您可以在以下网址找到相关信息:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html%23//apple_ref/doc/uid/TP40007457-CH111-SW1

我希望有所帮助。如果您有任何其他问题,请与我们联系!

干杯,埃文。

答案 3 :(得分:0)

朋友,顶栏是状态栏。你可以set.statusbar hidden = yes; 或者从plist更改它,当你打开你的plist时,有一个隐藏它的选项,