TabViewController+ UINavigationController

原文链接:[url]http://xiaoyu.li/2012/creat-navigationcontroller-and-tabbarcontroller-at-the-same-time/[/url]

同时创建navigationController和tabBarController

第一种方式是在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"];
}
return self;
}

-(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];

// 组装TabBar
self.viewControllers = [NSArray arrayWithObjects:firstController,secondController, nil];

[firstController release];
[secondController release];
}

return self;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值