UIViewController、UINavigationController与UITabBarController的整合使用
UINavigationController与UITabBarController是iOS开发中最常用的两种视图控制器,它们都属于UIViewController的子类,继承关系如下:
@interface UITabBarController : UIViewController <uitabbardelegate, nscoding="">
@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<uitabbarcontrollerdelegate, homedelegate="">
@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];
UIViewController、UINavigationController与UITabBarController的整合使用的更多相关文章
- iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController)
iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController) 前面我们介绍了StoryBoard这个新技术,和纯技术 ...
- iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController)
iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController) 这里我们就直接上实例: 一:新建一个项目singleV ...
- iOS开发——实战OC篇&环境搭建之StoryBoard(玩转UINavigationController与UITabBarController)
环境搭建之StoryBoard(玩转UINavigationController与UITabBarController) 研究了这么就IOS开发,都没有所处一个像样或者自己忙一点的项目.最近自 ...
- UINavigationController与UITabBarController相关问题
UINavigationController与UITabBarController相关问题 UINavigationController与UITabBarController混用是非常常见的,有时候会 ...
- UINavigationController和UITabBarController
UINavigationController和UITabBarController 目录 概述 UINavigationController UITabBarController 实用功能 待解决 概 ...
- UINavigationController 和 UITabBarController
UINavigationController当设置根控制器的时候,意思就是把根控制器压入栈内,当我们push的时候,我们把下一个控制器压入栈内,当我们pop的时候把上面的控制器的内存释放 UITa ...
- UINavigationController和UITabBarController合用
一.创建一个 Tabbed Application.默认创建的是带有两个Tab的工程. 二.在AppDelegate.h里面添加 @property (strong, nonatomic) UINav ...
- UINavigationController与UITabbarController的样式
之前虽然也手写过这两中视图控制器,但是更多的还是使用SB来创建,最近发现了一些问题,现在总结一下. 1.改变UINavigationBar的颜色 在UINavigationController中,之前 ...
- UINavigationController 与 UITabBarController
http://www.cnblogs.com/YouXianMing/p/3756904.html // index start from 1. UITabBarItem *newsItem = [[ ...
随机推荐
- Chapter 5. Label and Entry Widgets 标签和输入部件
Chapter 5. Label and Entry Widgets 标签和输入部件 有时候,你需要用户输入特定的信息,比如他们的名字,地址或者 甚至序号. 简单的方式来实现这个是使用Enry 部件 ...
- boost::asio 使用 libcurl
curl 使用 asio 的官方样例 http://curl.haxx.se/libcurl/c/asiohiper.html, 但这个例子用起来有很明细的 bug,asio 异步IO 只注册一次,也 ...
- wordpress All in one Seo
原文地址:http://www.7adesign.com/155.html WordPress插件All-in-one-seo-pack详细设置: I enjoy this plugin and ha ...
- stl_map,set 用法
set: 集合a,b加起来,去重 hdu 1406 #include <iostream> #include<cstdio> #include<set> using ...
- Objective-C 基本语法:实例变量与成员变量的区别.l........实例方法和类方法区别
http://leopard168.blog.163.com/blog/static/16847184420138153296930/ http://blog.csdn.net/thdxs/artic ...
- 网易云课堂_C++程序设计入门(上)_第1单元:C++概览_第1单元作业 - 写代码 - 互评 (难度:易)
第1单元作业 - 写代码 - 互评 (难度:易) 查看帮助 返回 提交作业(截止时间已过) 完成并提交作业 作业批改 互评训练 互评作业 自评作业 成绩公布 查看成绩 温 ...
- 聚类算法初探(五)DBSCAN
最近由于工作需要,对聚类算法做了一些相关的调研.现将搜集到的资料和自己对算法的一些理解整理如下,供大家参考. 另外在算法代码方面,我也做了一些实现(包括串行和并行),欢迎感兴趣的朋友探讨和交流. 第一 ...
- EXT.NET常用属性
Ext_数字输入框_Ext.form.NumberField: <mce:script type="text/javascript"><!-- /* Ext. ...
- 【Android】Fragment如何获取子Fragment
今天搞了个嵌套的Fragment,通过外部的Fragment获取的子Fragment代码: this.navigationBar = (HXKJCargoNavigationView) getFrag ...
- Qt5 文本编辑
[应用场景]:在编辑框中输入一段文字,用鼠标选取文字,修改工具栏上的字体.字号大小.加粗.斜体等属性,选取的文字即发生相应的变化. 一. 任何一个文本编辑器的程序都要用到QTextEdit ...