导航控制器---UINavigationController
来源:互联网 发布:淘宝首页html代码示例 编辑:程序博客网 时间:2024/03/29 09:17
·导航视图控制器的基本概念
·导航控制器(UINavigationController)是用来管理子控制器的切换的大管家
·导航控制器是个管理型的控制器,它本身不显示视图内容,而是管理子控制器的
视图显示
·UINavigationController是UIViewController的子类
导航控制器的基本样式
·基本样式
·蓝色部分为:导航控制器的导航栏(NavigationBar)
·橙色部分为:控制器包含的内容视图(用户感兴趣的区域)
实现导航控制器
·创建导航控制器
//创建子控制器
RootViewController *rootVC = [[RootViewController alloc] init];
//创建导航控制器,并且将rootVC 作为导航控制器管理的第一个子控制器
UINavigationController*navigation = [[UINavigationControlleralloc]
initWithRootViewController:rootVC];
·导航到下一个子控制器
//创建第二个子控制器
SecondViewController *secondVC = [[SecondViewController alloc] init];
//导航到第二个控制器
[self.navigationController pushViewController:secondVC animated:YES];
还可以通过storyboard实现,参见《》
导航控制器的结构图
·导航控制器结构
·导航栏(Navigation bar)
·子控制器的视图(Custom content)
·导航工具栏(Navigation toolbar),默认隐藏,较少使用
导航控制器中视图元素尺寸
·元素尺寸
导航控制器的层次关系
·导航控制器层次关系分析
栈结构管理子控制器
·栈结构特点:先进后出,后进先出
2、导航控制器常用属性与常用方法
·导航控制器常用属性
// 获取到在栈中最顶层的视图控制器
@property(nonatomic,readonly,retain) UIViewController *topViewController;
// 获取到在栈中当前显⽰的视图控制器
@property(nonatomic,readonly,retain) UIViewController *visibleViewController;
// 所有的⼦控制器
@property(nonatomic,copy) NSArray *viewControllers;
// 获取到导航栏
@property(nonatomic,readonly) UINavigationBar *navigationBar;
·常用方法
// 初始化一个根视图控制器,在栈的最底层
- (id)initWithRootViewController:(UIViewController *)rootViewController;
- (void)pushViewController:(UIViewController *)viewController
animated:(BOOL)animated;// 压入到一个新的视图控制器中,在栈中最顶层,可以选择是否需要
三种填出方式
//弹出到上一个控制器,可以选择是否需要动画效果
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
- (NSArray *)popToViewController:(UIViewController *)viewController
animated:(BOOL)animated;// 弹出到指定的视图控制器中,可以选择是否需要动画效果
// 回到根视图控制器,可以选择是否需要动画效果
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
3 、 UINavigationBar
·导航栏结构剖析图(类关系图)
·导航栏的基本概念
·一个导航控制器包含一个导航栏(UINavigationBar)
·一个子控制器对应一个导航项(UINavigationItem)
·导航控制器管理一系列子控制器,UINavigationBar 管理 一系列
UINavigationItem,每个UINavigationItem和一个子控制器对应
设置导航栏标题
·使用导航栏内置的标题
self.title= @"第一个视图控制器";
·自定义标题视图
UILabel *titleLabel = [[UILabel alloc] initWithFrame:
CGRectMake(0,0, 100, 44)];
titleLabel.backgroundColor = [UIColor redColor];
titleLabel.text = @"第一个视图控制器";
self.navigationItem.titleView= titleLabel;
UIBarButtonItem
·UIBarButtonItem是用来显示在导航栏左侧、右侧的按钮
UIBarButtonItem*rightItem = [[UIBarButtonItemalloc]
initWithTitle:@"item"
style:UIBarButtonItemStylePlain
target:self
action:@selector(buttonAction)];
self.navigationItem.rightBarButtonItem= rightItem;
·以下是UIBarButtonItem 初始化方法
// 初始化一个UIBarButtonItem的实例,初始化一个系统的Item
- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem
target:(id)target action:(SEL)action;
// 初始化一个带图片的UIBarButtonItem实例
- (id)initWithImage:(UIImage*)image style:(UIBarButtonItemStyle)style
target:(id)target action:(SEL)action;
// 初始化一个只带标题的UIBarButtonItem实例
- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style
target:(id)target action:(SEL)action;
// 初始化自定义的一个视图
- (id)initWithCustomView:(UIView*)customView;
设置导航栏
·设置风格
·设置颜色
// 设置导航栏的风格为黑色⿊⾊
self.navigationController.navigationBar.barStyle= UIBarStyleBlack;
// 设置导航栏为透明
self.navigationController.navigationBar.translucent= YES;
// 设置导航栏的颜色
self.navigationController.navigationBar.tintColor= [UIColor redColor];
//设置背景图片
[self.navigationController.navigationBar
setBackgroundImage:[UIImageimageNamed:@"img.png"]
forBarMetrics:UIBarMetricsDefault];
·设置prompt属性
导航栏的prompt属性,通过navigationItem来设置,其主要作用是用于提示用
户。比如,用户正在请求网络数据时,提示用户数据正在加载。待加载完成后可以
将它的值设置为nil,取消显示。
// 设置导航栏提示用户的内容
self.navigationItem.prompt= @"加载";
// 取消提示
self.navigationItem.prompt= nil;
4、导航控制器的委托方法
·设置代理方法
·导航控制器的委托方法UINavigationControllerDelegate,通过设置代理监听视图
控制器的切换。
self.navigationController.delegate= self;// 设置代理方法
// 视图控制器将要显示时调⽤
- (void)navigationController:(UINavigationController*)navigationController
willShowViewController:(UIViewController *)viewController animated:
(BOOL)animated;
// 视图控制器已经显示时调⽤
- (void)navigationController:(UINavigationController*)navigationController
didShowViewController:(UIViewController *)viewController animated:
(BOOL)animated
导航控制器小结
·小结
·一个UINavigationController对应一个NavigationBar实例
·一个UINavigationController可以包含多个子控制器UIViewController
·每一个子控制器UIViewController对应一个UINavigationItem实例
·UINavigationItem控制器多个UIBarButtonItem(2个)
摘自《无线互联3G学院》培训资料
- 导航视图控制器UINavigationController
- 导航控制器 ( UINavigationController )
- 导航控制器UINavigationController
- UINavigationController导航控制器详解
- UINavigationController导航控制器…
- UINavigationController导航控制器…
- UINavigationController导航控制器…
- UINavigationController 导航控制器
- 导航控制器UINavigationController
- 导航控制器:UINavigationController
- 自定义UINavigationController(导航控制器)
- swift UINavigationController 导航控制器
- UINavigationController 导航视图控制器
- UINavigationController 导航控制器
- UINavigationController 导航控制器
- UINavigationController导航控制器
- UI UINavigationController导航控制器
- UINavigationController---导航控制器
- 斐波那契数列
- 【Android实战】使用帧动画实现自定义loading加载布局
- Scala练习(十)
- 素数筛选 模版
- 线程 ,生产者. 消费者 互斥锁,条件变量
- 导航控制器---UINavigationController
- 加密算法比较3DES AES RSA ECC MD5 SHA1等
- html与css经典二级菜单编写2
- hdu 1198Farm Irrigation
- 常用的正则表达式大全
- android的camera
- Java基础----一其他类的总结(一)
- Edittext请求输入法无效的解决方案
- IK Analyzer 2012FF_hf1使用