【iOS】UIViewController、UINavigationController与UITabBarController的整合使用

原文  http://blog.csdn.net/rongxinhua/article/details/20214293

UINavigationController与UITabBarController是iOS开发中最常用的两种视图控制器,它们都属于UIViewController的子类,继承关系如下:

@interface UITabBarController : UIViewController
@interface UINavigationController : UIViewController

UINavigationController:同级页面之间的跳转,界面典型的特点就是页面上部有一UINavigationBar导航条,导航条可以设置标题、左上角的按钮(一般用于返回),右上角的按钮,也可以自定义这些元素。

UITabBarController:父子页面之间的嵌套关系,界面典型的特点是耍耍下部有一UITabBar选项组,通过点击Tab,可切换上面的视图的变换。  

UIViewController、UINavigationController、UITabBarController三者的整合使用,可以开发出大部分的App应用页面框架。

一、在我们项目AppDelegate中添加UIViewController

//把UIViewController添加的应用中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    SplashViewController *splashViewController = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
    self.window.rootViewController = splashViewController;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

上面说过,UINavigationController与UITabBarController是UIViewController的子类,所以,也可以按这样的方式添加,主要看项目界面的需要。

二、UIViewController之间的跳转与传参

一般的应用程序都不会只有一个页面,而页面之间的跳转,可以这样调用:

//从UIViewController跳转到另一个UIViewController
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[self presentViewController:loginViewController animated:true completion:^{}];
//从UIViewController返回上一个UIViewController
[self dismissViewControllerAnimated:true completion:^{}];

很多从Android开发转过来的同事,都会问一个问题:两个页面之间怎么传递参数?

其实,Android通过Intent对象来跳转和传递参数,当前页面是拿不到下一个页面的实例;而在iOS中,我们是通过直接创建的方式创建下一个页面实例的,所以,你可以在下一个UIViewController实例中提供一个方法,供当前页面去给它设置参数就行。

在Android中,返回上一个页面,还是通过Intent来回传参数;而在iOS中,可通过设置代理的方式来传参。具体使用下面的例子中会看到。

三、由UIViewController跳转到UITabBarController

//从UIViewController跳转到UINavigationController
HomeViewController *homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
[self presentViewController:navigationController animated:true completion:^{}];

HomeViewController是一个UITabBarController子类,代码如下:

//HomeViewController是一个UITabBarController子类
@interface HomeViewController : UITabBarController
@end

四、UITabBarController嵌套子页面

在本例中,这个UITabBarController还属于UINavigationController导航链中的一个环节,故可以调用导航控制器相应的方法。

UITabBarController本身可以嵌套多个子页面的,每个页面可以由一个UIViewController来提供。代码如下:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //设置导航标题
        self.title = @"Message";
        //设置导航左上角的按钮
        UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(onLeftBtnClick:)];
        self.navigationItem.leftBarButtonItem = leftBtn;
        //设置导航右上角的按钮
        UIImage *img = [UIImage imageNamed:@"msgIcon"];
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:self action:nil];
        self.navigationItem.rightBarButtonItem = rightBtn;

        //新建Tab页面
        UserListViewController *userListViewController = [[UserListViewController alloc] initWithNibName:@"UserListViewController" bundle:nil];
        MessageListViewController *messageListViewController = [[MessageListViewController alloc] initWithNibName:@"MessageListViewController" bundle:nil];
        //添加Tab耍耍到Tab控制器
        self.viewControllers = @[messageListViewController, userListViewController];
        //设置UITabBarControllerDelegate代理
        self.delegate = self;
    }
    return self;
}

五、UITabBarController子页面之间的切换 

HomeViewController实现了UITabBarControllerDelegate协议,可用于Tab切换时执行某此操作,如下:

//实现协议方法,用于切换Tab时,更改页面的标题
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSInteger index = tabBarController.selectedIndex;
    NSString *title;
    switch (index) {
        case 0:
            title = @"Message";
            break;
        case 1:
            title = @"User List";
            break;
    }
    self.title = title;
}

在UITabBarController的子页面(为UIViewController实例)中,可以设置该子页面所对应的TabBar项的相关属性,如下:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.tabBarItem.title = @"User List";
        self.tabBarItem.image = [UIImage imageNamed:@"chatIcon01"];
    }
    return self;
}

六、UITabBarController子页面跳转到UINavigationController的下一个页面

从UITabBarController子页面跳转到UINavigationController的下一个页面,注意:前提是UITabBarController是属于UINavigationController导航链中的一个节点。

//从UITabBarController的子页面跳转到UINavigationController的下一个页面:
ChatViewController *chatViewController = [[ChatViewController alloc] initWithNibName:@"ChatViewController" bundle:nil];
UITabBarController *homeController = self.tabBarController;
[chatViewController setHomeDelegate:homeController];
[self.tabBarController.navigationController pushViewController:chatViewController animated:true];

这里说一下用代理来实现参数的回传,这个代理的宝义如下:

@protocol HomeDelegate
-(void) onComeback:(NSString*) message;
@end

需要在下一个页面(例子中的ChatViewController)中,添加这个代理作为它的属性,如下:

@interface ChatViewController : UIViewController
@property (weak) id homeDelegate;
@end

@implementation ChatViewController
@synthesize homeDelegate;
@end

七、返回上一个页面与参数回传

要从ChatViewController返回到上一个页面,可执行下面代码:

[homeDelegate onComeback:@"Hello"];
[self.navigationController popViewControllerAnimated:true];

这样,就可以实现了参数的回传。 

UINavigationController的页面回退的两个常用方法:

//返回导航的上一个页面
[self.navigationController popViewControllerAnimated:true];

//返回导航的第一个页面
[self.navigationController popToRootViewControllerAnimated:true];
时间: 2024-08-22 19:25:17

【iOS】UIViewController、UINavigationController与UITabBarController的整合使用的相关文章

iOS应用开发中UITabBarController标签栏控制器使用进阶_IOS

做了这么长时间的ios开发了,最基本的UITabBarController和UINavigationController都用了好长时间了,总是改现成的代码,或者各种自定义控件的修改,用的都有些混乱了,呵呵.还是自己做个demo再复习一下吧,记录下来以备后续翻查.一.UITabBarController和UINavigationController的联合使用 这种方法最常见,好像一般有tabbar都会有naviBar.一般使用, 1. 在appDelegate里面创建UITabBarControl

iOS开发UI篇—UITabBarController简单介绍

一.简单介绍 UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就是QQ.微信等应⽤. 二.UITabBarController的使用 1.使用步骤: (1)初始化UITabBarController (2)设置UIWindow的rootViewController为UITabBarController (3)创建相应的子控制器(viewcontroller)

iOS 扩展 UINavigationController 出栈返回到先前标记的位置

iOS 扩展 UINavigationController 出栈返回到先前标记的位置 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 这一功能,是突然想到去年看一个 Java 什么数据结构的原码

UINavigationController与UITabBarController相关问题

UINavigationController与UITabBarController相关问题   UINavigationController与UITabBarController混用是非常常见的,有时候会遇到UINavigationController推出(push)出controller后隐藏UITabBarController的问题,很容易对吧. 源码如下: // // AppDelegate.m // NavigationController // // Copyright (c) 201

iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建)

一.UITabBarController在storyoard中得搭建 1.新建一个项目,把storyboard中默认的控制器删除,拖UITab Bar Controller. 2.创建viewcontroller,添加到UITab Bar Controller中去(连线). 注意点:连线的顺序就是将来显示的顺序,显示在眼前的为第一个连线的view. 提示:控制器的界面对应的tabbarbutton和图片显示什么内容,由它的控制器确定. 3.设置子控制器的UITabBar等信息. 4.运行效果 二

苹果推出“iOS in the Car”计划,整合多家汽车厂商车载系统

在 刚刚结束的苹果http://www.aliyun.com/zixun/aggregation/13010.html">全球 开发者大会WWDC上,苹果宣布了全 新的"iOS in the Car"计划,期望将iOS 7全面整合到各大主流汽车厂商的车载系统之中,目前已有包括本田.日产.法拉利.奔驰.雪佛兰在内的12个汽车品牌与之展开合作. 苹果宣布,"iOS in the Car"计划旨在提供一个全新的技术平台,让汽车驾驶者将iPhone的功能投射

iOS - UINavigationController

前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : UIViewController @available(iOS 2.0, *) public class UINavigationController : UIViewController UINavigationController:容器视图控制器的一种,称之为导航视图控制器,导航视图控制器固定高度是 44. 导航视图控制器中存放的是视图控制器. 导航条的颜色与

iOS - UITabBarController

前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding> @available(iOS 2.0, *) public class UITabBarController : UIViewController, UITabBarDelegate, NSCoding UITabBarController: 分栏视图控制器,在创建时,需要一次性

Twitter联合创始人多西:为iOS整合做准备

新浪科技讯 北京时间9月2日早间消息,Twitter联合创始人杰克·多西(Jack Dorsey)周四通过官方博客向Twitter第三方开发者发布了一封公开信,希望得到反馈,以便更好地为开发者提供服务. 多西写道:"我们想知道,你们希望从我们这里得到哪些额外的材料,帮助你们开发产品.提升渠道并扩大影响力." 多西还提到:"很快,只要有iPhone或iPad的地方,你们就能找到Twitter."他还写道:"通过Twitter与iOS 5之间即将展开的深度整合