UINavigationController学习笔记
http://site.douban.com/129642/widget/notes/5513129/note/187701199/
1-view controllers的关系:
Each custom view controller manages a distinct view hierarchy but a navigation controller coordinates the navigation between different view hierarchies.
2-Navigation Interface中的对象
你不能改变navigation controller的navigation bar和toolbar对象,navigation controller对象自己负责配置和显示它们。此外,navigation controller自动将自己设置为它自己的UINavigationBar对象的委托,并组织其它对象改变此关系。
Navigation stack是一个后进先出的custom view controller集合,由navigation controller负责管理。第一个被加入的会称为root view controller并且永远不会被移出stack。
topViewController指向stack顶部的view controller。
visibleViewController指向当前显示的view的controller。
这2者未必相同,有可能当先显示的view controller并未入stack。
你的主要职责是根据用户的行为,将新的view controllers push到stack中。
3-创建Navigation Interface
首先要决定在哪里使用navigation interface。
● Install it directly in your application’s main window.
● Install it as the root view controller of a tab in a tab bar interface.
● Install it as one of the two root view controllers in a split view interface. (iPad only)
● Present it modally or otherwise use it as a standalone view controller that you can display and dismiss as needed.
● Display it from a popover. (iPad only)
然后为Navigation Interface定义custom view controllers
加载Navigation Interface
[1] 拖拽一个navigation controller到IB的document window;
[2] 保存一个到navigation controller的IBOutlet引用;
[3] 设置navigation controller的root view controller,(在IB中)将其设置为自定义的类;
The class you choose should be the one responsible for displaying the highest-level data in your navigation hierarchy.
[4] 配置root view controller的view;
[5] 在applicationDidFinishLaunching:方法中,将你的navigation controller的view加入到main window;
. - (void)applicationDidFinishLaunching:(UIApplication *)application {
. [window addSubview:myNavigationController.view];
.
}
[6] 保存nib file;
编程创建navigation controller:
[1] 创建root view controller;
[2] 创建navigation controller,调用initWithRootViewController:方法;
[3] 增加navigation controller的view到window;
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UIViewController *rootController = [MyRootViewController alloc] init];
navigationController = [UINavigationController alloc]
initWithRootViewController:rootController];
[rootController release];
window = [UIWindow alloc] initWithFrame:[UIScreen mainScreen] bounds];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
}
修改Navigation Stack
[1] 显示下一层数据:pushViewController: animated:方法;把新的view controller放到stack顶,该controller负责显示新的界面;
[2] 返回体系中的上一个层次:popViewControllerAnimated:方法;
[3] 将App恢复到上次退出前的状态:当App启动时,你可以使用setViewControllers: animated:
方法,恢复navigation controller的状态。为了恢复app到原来的状态,你必须保存足够的状态信息来重新创建需要的view controllers。下次启动时,需要读取这些状态信息来重新创建这些view controllers。之后才调用setViewControllers: animated:方法;
[4] 返回到root view controller:popToRootView- ControllerAnimated:方法;此方法删除root view controller以外的所有的controllers;
[5] 退多层:popToViewController: animated:方法。应用场景:当用户进行了一系列操作,想要撤销这些操作的时候可以调用此方法;
[6] 跳到任意层次:setViewControllers: animated:方法,尽量别用;
4-监控Navigation Stack的变化
当navigation stack发生变化,navigation controller发送适当的消息给它的代理。
当新入stack一个view controller时,会向topmost view controller发送viewWillDisapear消息,向新的view controller发送viewWillAppear消息;
向委托发送navigationController: willShowViewController: animated:消息;
完成视图替换。
向topmost view controller发送viewDidDisappear:,向新的view controller发送viewDidAppear:消息;
向委托发送navigationController: didShowViewController: animated:消息;
5-定制Navigation Bar 外观
navigation bar是管理controls的容器视图。Navigation controller负责管理navigation bar。
[1] 配置Navigation Item Object
Navigation bar是采用navigation item stack保存UINavigationItem对象。每个navigation item提供一个完整的视图集合,以及在navigation bar中要显示的内容。navigation bar是一个纯粹的view对象,可以被嵌入到其它view中。UINavigationBar的委托是UINavigationController(如果尝试修改,会导致异常)。UINavigationBar中保存了以下指针:items、backItem、topItem。
当使用一个navigation interface,navigation bar的stack一般是和navigation controller的stack并行的。这是1对1的关系。
一个navigation bar有3个主要的位置用来放置items:左(backBarButtonItem-缺省,leftBarButtonItem-用以替换back按钮)、中(titleView)、右(rightBarButtonItem)。可以在rightBarButtonIte的contentView中定制右边的按钮,可以放按钮的集合。
[2] 显示和隐藏Navigation Bar
使用UINavigationController的setNavigationBarHidden:animated:方法显示或隐藏navigation bar。不能修改UINavigationBar对象的hidden属性。在viewWillAppear:中调用。
[3] 修改Navigation Bar
对于navigation interface,一个navigation controller拥有它自己的UINavigationBar对象,并且负责管理它。不允许改变navigation bar对象的bounds,frame,或者alpha等等。但是以下几个属性允许修改,包括:barStyle,translucent,tintColor属性。
[4] 使用用户自定义Buttons和Views做Navigation Items
你可以通过view controller的navigationItem属性,得到navigation item。view controller在你请求之前,不会创建它的navigation item。如果你不修改view controller的navigation item, navigation item会提供一系列缺省对象(可以满足多数需求)。当然,你定制的内容要优先于缺省对象。
左侧区域的规则:
1. 如果你配置了一个自定义的bar button item到( topmost view controller’s navigation item的)leftBarButtonItem属性,那么此item会得到最高优先级;
2. 如果你没有配置bar button item(既没有做1.的操作),而且在navigation stack中处于当前view controller的下层的view controller的navigation item在其backBarButtonItem属性中有一个正确的item,navigation bar将显示该item;
3. 如果1.2.都没有配置那么会自动配置一个缺省的back button,其title背设置为前一个view controller的title属性。
中间区域的规则:
1. 如果你配置了一个自定义view到topmost view controller’s navigation item的titleView,navigation bar 将显示该view;
2. 如果没做1.的配置,navigation bar 会显示一个定制的view,包含view controller(注意,和1不同,1是navigation item的titleView)的navigation item的title,如果title是nil,则显示view controller自己的title;
右侧区域的规则:
1. 如果新的top-level view controller有一个定制的right bar button item,将显示该item。通过设置navigation item的rightBarButtonItem属性来定制;
2. 如果没有定制right bar button item,将什么都不显示;
示例代码(viewDidLoad):
UISegmentedControl *segmentedControl = [UISegmentedControl alloc] initWithItems: [NSArray arrayWithObjects:
[UIImage imageNamed:@"up.png"],
[UIImage imageNamed:@"down.png"],
nil];
[segmentedControl addTarget:self action:@selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
defaultTintColor = [segmentedControl.tintColor retain]; for later
UIBarButtonItem *segmentBarItem = [UIBarButtonItem alloc]
initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
[5] 使用Edit和Done Buttons
UIViewController的editButtonItem方法返回一个预配置的button,按下此button会调用view controller的setEditing:animated:方法。增加此按钮:
myViewController.navigationItem.rightBarButtonItem = [myViewController editButtonItem];
另外要重写setEditing:animated:方法。
6-显示Navigation Toolbar
toolbar由navigation controller对象。
[1] 通过设置navigation controller的toolbarHidden属性为NO来显示boolbar。
[2] 配置UIBarButtonItem对象数组到toolbarItems属性(每个需要toolbar的view controller都要单独定制)。
7-定义Toolbar Items
当配置bar button items时, 要为button配置一个合适的target(一般是view controller自身)和action。
示例代码:
. - (void)configureToolbarItems
. {
.
UIBarButtonItem *flexibleSpaceButtonItem = [UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// Create and configure the segmented control
UISegmentedControl *sortToggle = [UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:@"Ascending",
@"Descending", nil];
sortToggle.segmentedControlStyle = UISegmentedControlStyleBar;
sortToggle.selectedSegmentIndex = 0;
[sortToggle addTarget:self action:@selector(toggleSorting:)
forControlEvents:UIControlEventValueChanged];
// Create the bar button item for the segmented control
UIBarButtonItem *sortToggleButtonItem = [UIBarButtonItem alloc]
initWithCustomView:sortToggle];
[sortToggle release];
// Set our toolbar items
self.toolbarItems = [NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
nil];
[sortToggleButtonItem release];
[flexibleSpaceButtonItem release];
}
可以通过setToolbarItems:animated:方法改变toolbar的设置。
显示和设置Toolbar
为了隐藏一个特定view controller的toolbar,设置hidesBottomBarWhenPushed属性为YES。(这个是在切换view controller时使用的方法)
当view controller替换完了,可以调用setToolbarHidden:animated:方法。
UINavigationController学习笔记的更多相关文章
- iOS学习笔记-自己动手写RESideMenu
代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...
- IOS学习笔记37——ViewController生命周期详解
在我之前的学习笔记中讨论过ViewController,过了这么久,对它也有了新的认识和体会,ViewController是我们在开发过程中碰到最多的朋友,今天就来好好认识一下它.ViewContro ...
- React Native 学习笔记--进阶(二)--动画
React Native 进阶(二)–动画 动画 流畅.有意义的动画对于移动应用用户体验来说是非常必要的.我们可以联合使用两个互补的系统:用于全局的布局动画LayoutAnimation,和用于创建更 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- seaJs学习笔记2 – seaJs组建库的使用
原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...
随机推荐
- ThinkPHP中initialize和construct调用父类的区别
http://blog.topok.net/archives/142 需要加parent::_initialize();
- 【正则】精通JS正则表达式,没消化 信息量太大,好文
http://www.jb51.net/article/25313.htm 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用 ...
- centos crontab 定时任务详解
安装crontab: yum install crontabs 说明: /sbin/service crond start //启动服务 /sbin/service crond stop //关闭服务 ...
- 获取不到app.config里面的数据库连接字符串的解决方法
今天在自己的类库里添加了对app.config文件的数据库连接字符串的引用,但是返回的居然是Null,纳闷了.然后在网上找到了答案原来是我的app.config文件加错了地方,应该加到启动项目里面,而 ...
- Codeforces Round #347 (Div. 2) B. Rebus
题目链接: http://codeforces.com/contest/664/problem/B 题意: 给你一个等式,把等式左边的问号用1到n(n为等式右边的数)的数填好,使得等式成立 题解: 贪 ...
- Windows下JNI执行步骤
问题描述: java JNI使用 问题解决: JNI编写步骤: (1)编写带有native声明的方法的java类 (2)使用javac编译所写的java类,然后使用javah生成扩展名 ...
- int a[5]={1,2,3,4,5}; int *p=(int*)(&a+1); printf("%d",*(p-1)); 答案为什么是5?
这个问题的关键是理解 &a a是一个数组名,也就是数组的首地址.对a进行取地址运算符,得到的是一个指向数组的指针!!!!这句话尤为重要!也就相当于int (*p) [5] = &a;p ...
- uva 11375
思路是刘书上的 但是个高精度 java 大数 ~~ import java.util.*; import java.io.*; import java.math.BigInteger; public ...
- xml存储图片 二进制存储图片
一.保存图片到XML文件 /// <summary> /// 保存图片到XML文件 /// </summary> private void UploadImageToXml() ...
- 中国移动MM Android/OPhone付费SDK--MMBillingSDK,集成问题总结
原地址:http://www.j2megame.com/html/xwzx/ty/2916.html 中国移动MM Android/OPhone付费SDK--MMBillingSDK,集成问题总结 近 ...