iOS:视图切换的第二种方式:UINavigationController导航栏控制器
UINavigationController:一个以栈的形式管理多视图的容器,负责子控制器之间的跳转。由于以栈的方式管理视图,各个视图的切换就是压栈和出栈操作,所以出栈后的视图会立即销毁。
介绍:



[c1.navigationController pushViewController:c2 animated:YES];
<7>
纯代码创建多视图的步骤:
1、首先将项目中的storyboard删除,并将项目栏General中Main interface的设置取消。
2、在应用程序代理类中创建window并设置它的frame,一般将window的大小设置为单例对象屏幕的大小,然后给window设置根控制器。
3、创建UINavigationController控制器,它是一个容器,用- (instancetype)initWithRootViewController:(UIViewController *)rootViewController方法初始化,传入一个UIViewController实例作为根控制器,这个根控制器永远处于栈底(如果栈中只有一个对象,那么根控制器也处于栈顶),将某个UIViewController压入栈时,控制器的视图会从窗口右侧滑入;出栈时,栈顶的控制器会被移除,其下的控制器的视图会从窗口左侧滑入
3、如果还要创建其他多个控制器,创建后,必须要获取当前栈中的根控制器,根控制器用- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animate方法将这些控制器依次推入栈中。
4、针对每一个控制器,在它们的类中分别设置它们的导航栏标题和导航条navigationItem中的属性,例如添加按钮、设置标题视图等,添加按钮时,给按钮初始化时可以设置类型,并添加事件,在事件中用方法:- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated推出栈中它的前一个控制器。
纯代码创建举例如下:
显示结果为:

文件截图为:

取消general中的Main interface设置


代码如下:
应用程序代理类中创建window和根控制器
#import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建window
self.window = [[UIWindow alloc]init]; //设置window的大小
CGSize screenSize = [[UIScreen mainScreen]bounds].size;
self.window.frame = CGRectMake(, , screenSize.width, screenSize.height); //为window设置根控制器
ViewController *VC = [[ViewController alloc]init];
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:VC]; self.window.rootViewController = naVC; //接收用户输入并显示
[self.window makeKeyAndVisible]; return YES;
} - (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
} - (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} - (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
} - (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} - (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} @end
在设置的根控制器类中设置导航栏属性,并将新的视图控制器推入栈中
#import "ViewController.h"
#import "secondViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor yellowColor]]; //设置导航栏
//设置标题
self.navigationItem.title = @"First"; //设置返回按钮
self.navigationItem.backBarButtonItem.title = @"first"; //设置右边的按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addClicked:)];
} -(void)addClicked:(UIBarButtonItem*)sender
{
secondViewController *secondVC = [[secondViewController alloc]init]; //将新的控制器压入导航控制器
[self.navigationController pushViewController:secondVC animated:YES];
} @end
在新的视图控制器类中设置导航栏属性,将当前控制器弹出栈中
#import "secondViewController.h"
@interface secondViewController ()
@end
@implementation secondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
//设置导航栏
//1、设置标题
self.navigationItem.title = @"Second";
//2.设置左边的按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Return" style:UIBarButtonItemStylePlain target:self action:@selector(backClicked:)];
}
-(void)backClicked:(UIBarButtonItem*)sender
{
//将当前的控制器弹出导航控制器
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
iOS:视图切换的第二种方式:UINavigationController导航栏控制器的更多相关文章
- iOS:视图切换的第一种方式:模态窗口
一.UIModalController:模态窗口(一个控制器模态出另一个控制器的模态窗口) 当我们在view controller A中模态显示view controller B的时候,A就充当pre ...
- iOS:切换视图的第三种方式:UITabBarController标签栏控制器
UITabBarController:标签栏控制器 •通过设置viewControllers属性或者addChildViewController方法可以添加子控制器 –NSArray *viewCon ...
- ios视图切换之push与present混用
在变成过程中,经常遇到两个视图控制器之间的切换,导航控制器即UINaVigation是最常用的一种,有时为了某些效果又需要进行模态切换,即present. 我们的布局经常是在window上加一个nav ...
- 【原】react做tab切换的几种方式
最近搞一个pc端的活动,搞了一个多月,甚烦,因为相比于pc端,更喜欢移动端多一点.因为移动端又能搞我的react了. 今天主要总结一下react当中tab切换的几种方式,因为tab切换基本上都会用到. ...
- IOS文件操作的两种方式:NSFileManager操作和流操作
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- 【百度地图API】关于如何进行城市切换的三种方式
原文:[百度地图API]关于如何进行城市切换的三种方式 摘要:本文介绍了三种切换城市的方式:查询城市.城市列表和显示城市轮廓. ------------------------------------ ...
- jQuery Mobile页面跳转切换的几种方式
jQuery Mobile在移动开发中越来越受到欢迎. 而他的各个版本号也在持续不断的更新中.相同的我也非常喜欢它,它加快了我们开发HTML5的速度. 同一时候又具备jQuery一样的操作方法. 学起 ...
- iOS 收起键盘的几种方式
iOS 收起键盘的几种方式 1.一般的view上收起键盘 // 手势 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ ...
- iOS拨打电话的三种方式
iOS拨打电话的三种方式 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 1 2 var string = "tel:" + "1 ...
随机推荐
- Java学习(简介,软件安装)
1. Java概述: Java的发展可以归纳如下的几个阶段. (1)第一阶段(完善期):JDK 1.0 ( 1995年推出)一JDK 1.2 (1998年推出,Java更名为Java 2): (2)第 ...
- css让元素不可点击 pointer-events: none;
张鑫旭大神:http://www.zhangxinxu.com/wordpress/2011/12/css3-pointer-events-none-javascript/ 我们知道form元素里的 ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]
题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...
- C#多线程编程实战(一):线程基础
1.1 简介 为了防止一个应用程序控制CPU而导致其他应用程序和操作系统本身永远被挂起这一可能情况,操作系统不得不使用某种方式将物理计算分割为一些虚拟的进程,并给予每个执行程序一定量的计算能力.此外操 ...
- Tweet信息搜集工具tinfoleak
Tweet信息搜集工具tinfoleak 推特是国外用户常用的社交网站.通过分析用户发布的推文以及社交活动,可以获取大量的个人信息.Kali Linux新增一款Tweet信息搜索工具tinfole ...
- 深入理解javascript作用域系列第二篇
前面的话 大多数时候,我们对作用域产生混乱的主要原因是分不清楚应该按照函数位置的嵌套顺序,还是按照函数的调用顺序进行变量查找.再加上this机制的干扰,使得变量查找极易出错.这实际上是由两种作用域工作 ...
- 【概率】【找规律】hdu6229 Wandering Robots
题意:一个机器人在正方形迷宫的左上角,迷宫里有些格子有障碍物,每一步机器人会等概率地向能走的格子转移(包含自身).问你无限长的时间之后,机器人处于矩形对角线的右下方的概率. 无限长时间意味着,起点没有 ...
- 53.FIB词链
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description Fibonacci词定义如下:FIB1 = b FIB2 = a 当k ...
- 同步IO与同步非阻塞IO的理解
本文图片均来自网络 一.同步IO---Blocking IO 在Blocking IO模型中,用户空间的应用程序执行一个系统调用(recvform),这会导致应用程序阻塞,直到数据准备好,并且将数据从 ...
- [转] Spring MVC 4.1.3 + MyBatis 零基础搭建Web开发框架
首先感谢一下润和软件,指引我走上了Spring MVC Web开发的道路. 下面进入正题 搭建开发环境: Netbeans8.0.2 + MySql5.6 + JDK1.7 + tomcat8.0.1 ...