---
title: "新风作浪博客学习(十六)Navigation + Tab Bar 常用组合框架 ."
author: "android_zhang"
date: "2013-06-07T08:50:49Z"
tags: ["iOS"]
categories: ["移动开发"]
slug: "navigation-tab-bar"
draft: false
lastmod: "2013-01-20T12:49:18Z"
id: 1772703
url: "https://android-zhang.iteye.com/blog/1772703"
---

看到很多项目中都采用的是Navigation加Tab Bar组合到一起，完成视图切换操作，在导航栏上添加基本按钮，给予响应事件，让应用给用户更好的体验，所以本菜鸟写了这个这样一个Demo，仅供学习

所创建工程模板是最后一个 Empty Application

![](http://dl.iteye.com/upload/attachment/0079/5489/724ed4c2-baaa-3816-96f4-52d8ec457918.png)



先看运行效果:
第一个视图，点击按钮切换视图，点击导航栏上按钮可以切换回去


![](http://dl.iteye.com/upload/attachment/0079/5491/f776d646-b182-3884-843a-fbdef1c1589e.png)



![](http://dl.iteye.com/upload/attachment/0079/5493/73d3ad50-efa7-364c-9e6e-4306b37e97d9.png)


第二个视图设置了背景颜色和透明度  第三个视图添加了背景图片


![](http://dl.iteye.com/upload/attachment/0079/5495/490cabf2-15ad-3431-9661-ff0cf82f63fb.png)



![](http://dl.iteye.com/upload/attachment/0079/5497/a7f5bfc9-833a-3844-aeb1-64ec93e62714.png)


第四个视图，在导航栏上添加了两个按钮，左边按钮属于自定义标题，右边按钮是系统的图标，点击左按钮弹出一个警告，右边按钮没有添加响应事件，点击后没反应


![](http://dl.iteye.com/upload/attachment/0079/5499/b0b22bcd-f9c5-33a1-8a23-60602318ebd4.png)



![](http://dl.iteye.com/upload/attachment/0079/5501/2c32c67b-be48-313f-a676-e1ef36b8221f.png)



Tab Bar上添加的都是自定义图片




框架组合的主要代码，在AppDelegate.m中

```java
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[uiwindow alloc] initWithFrame:[[uiscreen mainscreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [uicolor whitecolor];
    
    _tabBarController = [[uitabbarcontroller alloc] init];
    _tabBarController.delegate = self;
    

    FirstViewController *firstViewController = [[firstviewcontroller alloc] initWithNibName:@"FirstViewController" bundle:nil];
    
    SecondViewController *secondViewController = [[secondviewcontroller alloc] initWithNibName:@"secondViewController" bundle:nil];
    
    ThirdViewController *thirdViewController = [[thirdviewcontroller alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    
    FourViewController *fourViewController = [[fourviewcontroller alloc] initWithNibName:@"FourViewController" bundle:nil];
    
    UINavigationController *navFirst = [[uinavigationcontroller alloc] initWithRootViewController:firstViewController];
//    在加载图片是把标题都覆盖了，所以运行效果并没有显示这些文字
    navFirst.title = @"第一个视图";
    
    UINavigationController *navSecond = [[uinavigationcontroller alloc] initWithRootViewController:secondViewController];
    navSecond.title = @"第二个视图";
    
    UINavigationController *navThird = [[uinavigationcontroller alloc] initWithRootViewController:thirdViewController];
    navThird.title = @"第三个视图";
    
    UINavigationController *navFour = [[uinavigationcontroller alloc] initWithRootViewController:fourViewController];
    navFour.title = @"第四个视图";
    
    
    _tabBarController.viewControllers = [nsarray arraywithobjects:navfirst,navsecond,navthird,navfour, nil];
    _window.rootViewController = _tabBarController;
    
    [self.window addsubview:firstviewcontroller.view];
    [self.window makekeyandvisible];
    return YES;
}
```



第一个视图切换按钮响应事件

```java
- (IBAction)switchView:(id)sender {
    
    FirstViewController *firstController = [[firstviewcontroller alloc] init];
    [self.navigationcontroller pushviewcontroller:firstcontroller animated:yes];
    firstController.title = @"第一个视图另一个视图";
}
```




第四个视图添加两个按钮方法，在最后一个控制机的.m文件中的-(void)viewDidLoad方法中

```java
- (void)viewDidLoad
{
    [super viewdidload];
	// Do any additional setup after loading the view.
    UIBarButtonItem *leftButton = [[uibarbuttonitem alloc] initWithTitle:@"测试"
                                                                   style:UIBarButtonItemStylePlain target:self
                                                                  action:@selector(pullWarn)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UIBarButtonItem *rightButton = [[uibarbuttonitem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
    self.navigationItem.rightBarButtonItem = rightButton;
}
```