【转】同时创建navigationController和tabBarController

本文介绍两种在iOS应用中同时实现TabBar与NavigationBar的方法。第一种是在AppDelegate中设置TabBarController,并为每个子控制器配置NavigationController;第二种是在初始视图控制器中动态创建并管理TabBar。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

上次说了如何通过代码创建TabBar,但是在这一过程中我遇到一个困难,就是又要创建navigationBarController又要创建TabBarController,所以这就比较纠结了。不过经过一番Google之后,还是解决了这个问题,所以在这也就写一下,当做自己总结了。如果有错误还请提出:

 

第一种方式是在AppDelegate中将tabBarController作为subView,然后再在tabBarController的基础上增加navigationController,代码如下:
在applicationDidFinishLauchingWithOptions中加入以下代码:

 

-(BOOL)application:(UIApplication*)applicationDidFinishLauchingWithOptions:(NSDictionary*)lauchingOptions
{self.window =[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];self.window.backgroundColor =[UIColor whiteColor];UITabBarController*tabBarController =[[[UITabBarController alloc] init]autorelease];FirstViewController*firstController =[[FirstViewController alloc] initWithNibName:nil bundle:nil];UINavigationController*firstNavigation =[[[UINavigationController alloc] initWithRootViewController:firstController]autorelease];[firstController release];SecondViewController*secondController =[[SecondViewController alloc] initWithNibName:nil bundle:nil];UINavigationController*secondNavigation =[[[UINavigationController alloc] initWithRootViewController:secondController]autorelease];[secondController release];

    tabBarController.viewControllers =[NSArray arrayWithObjects:firstNavigation,secondNavigation,nil];[window addSubview:tabBarController.view];[window makeKeyAndVisible];}

 

这样之后分别在FirstViewController.m和SecondViewController.m里面加入如下代码:

 

-(id)init
{self=[super initWithNibName:nil bundle:nil];if(self){UITabBarItem*tabBarItem =[self tabBarItem];[tabBarItem setTitle]=@"xxx";[tabBarItem setImage]=[UIImage imageNamed:@"xxxxx"];}returnself;}-(id)initWithNibName:(NSString*)nibName bundle:(NSBundle*bundle){return[self init];}

 

这样的话,就创建了既有navigationBar又有TabBar的应用了,而且这种方法是tabBar在每个界面都是存在的。

 

第二种方法,仅仅是在一个页面上显示tabBar,这种方法不像上一种,需要将两个tabBar的界面放在一个根视图(假设为InitialViewController),代码如下:
在applicationDidFinishLauchingWithOptions中加入以下代码:

 

-(BOOL)application:(UIApplication*)applicationDidFinishLauchingWithOptions:(NSDictionary*)lauchingOptions
{self.window =[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];self.window.backgroundColor =[UIColor whiteColor];InitialViewController*initial =[[[InitialViewController alloc]init]autorelease];UINavigationController*navigationController =[[[UINavigationController alloc]initWithRootViewController:initial]autorelease];[self.window addSubview:navigationController.view];[self.window makeKeyAndVisible];}

 

然后在InitialViewController.m中import需要加载到tabBar的ViewController,加上如下方法:

 

-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{if(self=[super initWithNibName:nil bundle:nil]){FirstViewController*firstController =[[FirstViewController alloc]init];UIImage*image1 =[UIImage imageNamed:@"tabBar1.png"];
        firstController.tabBarItem =[[[UITabBarItem alloc] initWithTitle:@"xxxx" image:image1 tag:0]autorelease];SecondViewController*secondController =[[SecondViewController alloc] init];UIImage*image2 =[UIImage imageNamed:@"tabBar2.png"];
        secondController.tabBarItem =[[[UITabBarItem alloc] initWithTitle:@"xxxx" image:image2 tag:0]autorelease];// 组装TabBarself.viewControllers =[NSArray arrayWithObjects:firstController,secondController,nil];[firstController release];[secondController release];}returnself;}

 

好了,就这些,结束。

转自:http://xiaoyu.li/2012/creat-navigationcontroller-and-tabbarcontroller-at-the-same-time/#comment-116

转载于:https://www.cnblogs.com/learnios/archive/2012/08/24/2654570.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值