【iOS】UIViewController、UINavigationController与UITabBarController的整合使用
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 initializationself.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];
【iOS】UIViewController、UINavigationController与UITabBarController的整合使用相关推荐
- UIViewController、UINavigationController与UITabBarController的整合使用
UINavigationController与UITabBarController是iOS开发中最常用的两种视图控制器,它们都属于UIViewController的子类,继承关系如下: @interf ...
- iOS开发UI篇—UITabBarController简单介绍
iOS开发UI篇-UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...
- 文顶顶 iOS开发UI篇—UITabBarController简单介绍 iOS开发UI篇—UITabBarController简单介绍...
一.简单介绍 UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例 ...
- iOS中UINavigationController控制器使用详解
一.概述 UINavigationController用来管理视图控制器,在多视图控制器中常用.它以栈的形式管理视图控制器,管理视图控制器个数理论上不受限制(实际受内存限制),push和pop方法来弹 ...
- UINavigationController与UITabbarController的样式
之前虽然也手写过这两中视图控制器,但是更多的还是使用SB来创建,最近发现了一些问题,现在总结一下. 1.改变UINavigationBar的颜色 在UINavigationController中,之前 ...
- iOS UIViewController和UIView的生命周期
文章目录 UIViewController的生命周期 UIView的生命周期 UIViewController和UIView进入展示时整体生命周期调用顺序 开发技巧总结 UIViewControlle ...
- ios 自己服务器 苹果支付_thinkphp整合系列之苹果AppStore内购付款的服务器端php验证...
如果要演一部霸道总裁的剧: 我想这主角必须非苹果莫属了: 苹果的霸道实在是出了名的: 这不:如果是非实物的交易: 现在你不给苹果交个过路费: 那都是立马被审核处死的节奏: 好了:以上仅为吐槽: 正题: ...
- 洪小瑶学iOS-UINavigationController
1.UINavigationController导航控制器如何使用 UINavigationController可以翻译为导航控制器,在iOS里经常用到. 我们看看它的如何使用: 下面的图显示了导航控 ...
- IOS UIViewController API,生命周期详解
在UIKit框架中只有一个根制图控制器.视图控制器用来管理界面和处理界面的类对象,程序启动前必须创建根视图控制器.所有视图控制器都需要自定义完成,必须继承于UIViewController 实现.xc ...
最新文章
- FPGA之道(7)时钟网络资源
- Oracle中rank() over, dense_rank(), row_number() 的区别
- mysql iops_MySQL实例IOPS使用率高的原因和解决方法
- Scss、elementUI引入、transition动画 - 学习笔记
- 上海师范大学计算机作业网址,新SQL——SERVER实验练习答案.doc
- 信息学奥赛之数学一本通_部分地区中考加分,又一批中学公布中考认可信息学特长生!...
- 微信开发者工具在线调试
- sql如何在两张表中得到每组数据,并知道数据的个数,举例,判断有多少班级,每个班的人数
- 第四卷 风起海外 第三百九十四章 修士、妖兽、小岛
- SecureCRT绿色版的下载和安装
- Generative Image Inpainting with Contextual Attention(CVPR2018)
- postgreSQL 获取当前连接的IP
- 利用python代码 之将指定网易云歌单保存到Excel中
- U盘git仓库快速拷贝
- Linux搭建samba服务及使用案例
- 词法分析扫描器的设计实现
- AI MAX交互式开发使用方法说明(配合xshell)
- 基于AForge的C#摄像头视频录制
- 齐治堡垒机前台远程命令执行漏洞(CNVD-2019-20835)分析
- 查找:顺序查找、二分查找、分块查找
热门文章
- 《信息处理技术员考试考前冲刺预测卷及考点解析》下午案例复习重点
- CISA:警惕俄罗斯 “Sandworm” 黑客组织使用的新型恶意软件框架
- 2019最有意思的五大 ZDI 案例之:通过调色板索引实现 Win32k.sys 本地提权漏洞 (下)...
- 【PHP基础】文件操作
- 注解之RetentionPolicy,ElementType
- 用Aliyun E-MapReduce集群的sqoop工具和数据库同步数据如何配置网络
- Qt Model/View/Delegate浅谈 - QAbstractListModel
- 多线程编程(2): 线程的创建、启动、挂起和退出
- Python nose test framework 介绍
- python代码性能优化技巧