iOS学习笔记(6)UINavigationController
2015-07-02 20:30
330 查看
UINavigationController/导航控制器
【注】导航控制器,是UIKit框架提供的一个容器视图控制器,用于切换拥有明确层次关系的视图。即由一级视图切换到二级视图,而不是平级视图间的切换。
【注】导航中视图控制器的层次结构,称为栈结构。
导航控制器通过栈这种数据结构来管理视图控制器(导航控制器有一个栈容器)
栈是一种常用的数据结构 (栈中元素遵循先进后出的原则)
/*1、第一个被添加到栈中的元素叫做基栈
*2、最后一个被添加到栈中的元素叫做栈顶
*3、将元素添加到栈中的操作叫做元素的入栈
*4、移除栈中元素的操作,叫做元素的出栈
*/
UINavigationBar (导航条,普通视图控件,父类为UIView)
每个导航控制器有且只有一个导航条(所有的视图控制器共用一个导航条)
拿到导航条的方法
self.navigationController.navigationBar
1.定义导航栏(左右Item)
//定义左边的item(系统的)
//第一个参数是样式(item的样式)
//第二个接收消息的对象
//第三个是触发事件
UIBarButtonItem *leftItem1=[[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self
action:@selector(leftItem1OnClick:)];
//把我们定制的item赋值给我们的Controller上的item(左边的)属于Controller的属性
self.navigationItem.leftBarButtonItem=leftItem1;
//定制右边的item
UIBarButtonItem *rightItem=[[UIBarButtonItem
alloc] initWithTitle:@"下一页"
style:UIBarButtonItemStylePlain
target:self
action:@selector(rightItemOnClick:) ];
self.navigationItem.rightBarButtonItem=rightItem;
[leftItem1
release];
[rightItem
release];
//左边第一个item的方法
-(void)leftItem1OnClick:(UIBarButtonItem*)buttonItem
{
NSLog(@"左边第一个按钮点击了");
}
//右边item的方法
-(void)rightItemOnClick:(UIBarButtonItem*)buttonItem
{
NSLog(@"右边按钮点击了");
SecondViewController *second=[[SecondViewController
alloc]
init];
[self.navigationController
pushViewController:second animated:YES];
[second
release];
}
2.切换视图
[self.navigationController
pushViewController:second animated:YES];
second 要切换的视图
pushViewController:second 就相当于入栈,
========================================
//右边item的方法
-(void)rightItemOnClick:(UIBarButtonItem*)buttonItem
{
NSLog(@"右边按钮点击了");
SecondViewController *second=[[SecondViewController
alloc]
init];
[self.navigationController
pushViewController:second animated:YES];
[second
release];
}
==========================================
popViewControllerAnimated:YES
相当出栈
==========================================
可与直接切换到根视图,中间的视图一次从栈中移除
//返回根视图
[self.navigationController
popToRootViewControllerAnimated:YES];
=======================================================
可以切换到指定的视图
[self.navigationController
popToViewController:self.navigationController.viewControllers[1]
animated:YES];
3.自定义Title
可以通过self.title=@"首页”;设置title
UINavigationController的title可以用别view替代,比如用UIButton
UILable等
UILabel *label=[[UILabel
alloc] initWithFrame:CGRectMake(0,
0, 100,
40)];
label.text=@"首页";
label.font=[UIFont
systemFontOfSize:18];
label.textColor=[UIColor
magentaColor];
label.textAlignment=NSTextAlignmentCenter;
self.navigationItem.titleView=label;
[label
release];
4.显示TooBar
在导航视图控制器中,工具栏默认是隐藏的,使用时先让其显示
[self.navigationController setToolbarHidden:NO animated:YES];
可以在工具栏中添加UIBarButtonItem
//toobarItem属于我们controller
UIBarButtonItem *toolbarItem=[[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self
action:@selector(toolbarItemClick:)];
self.toolbarItems=@[toolbarItem];
}
-(void)toolbarItemClick:(UIBarButtonItem*)buttonItem
{
NSLog(@"照相了");
}
【注】导航控制器,是UIKit框架提供的一个容器视图控制器,用于切换拥有明确层次关系的视图。即由一级视图切换到二级视图,而不是平级视图间的切换。
【注】导航中视图控制器的层次结构,称为栈结构。
导航控制器通过栈这种数据结构来管理视图控制器(导航控制器有一个栈容器)
栈是一种常用的数据结构 (栈中元素遵循先进后出的原则)
/*1、第一个被添加到栈中的元素叫做基栈
*2、最后一个被添加到栈中的元素叫做栈顶
*3、将元素添加到栈中的操作叫做元素的入栈
*4、移除栈中元素的操作,叫做元素的出栈
*/
UINavigationBar (导航条,普通视图控件,父类为UIView)
每个导航控制器有且只有一个导航条(所有的视图控制器共用一个导航条)
拿到导航条的方法
self.navigationController.navigationBar
1.定义导航栏(左右Item)
//定义左边的item(系统的)
//第一个参数是样式(item的样式)
//第二个接收消息的对象
//第三个是触发事件
UIBarButtonItem *leftItem1=[[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
target:self
action:@selector(leftItem1OnClick:)];
//把我们定制的item赋值给我们的Controller上的item(左边的)属于Controller的属性
self.navigationItem.leftBarButtonItem=leftItem1;
//定制右边的item
UIBarButtonItem *rightItem=[[UIBarButtonItem
alloc] initWithTitle:@"下一页"
style:UIBarButtonItemStylePlain
target:self
action:@selector(rightItemOnClick:) ];
self.navigationItem.rightBarButtonItem=rightItem;
[leftItem1
release];
[rightItem
release];
//左边第一个item的方法
-(void)leftItem1OnClick:(UIBarButtonItem*)buttonItem
{
NSLog(@"左边第一个按钮点击了");
}
//右边item的方法
-(void)rightItemOnClick:(UIBarButtonItem*)buttonItem
{
NSLog(@"右边按钮点击了");
SecondViewController *second=[[SecondViewController
alloc]
init];
[self.navigationController
pushViewController:second animated:YES];
[second
release];
}
2.切换视图
[self.navigationController
pushViewController:second animated:YES];
second 要切换的视图
pushViewController:second 就相当于入栈,
========================================
//右边item的方法
-(void)rightItemOnClick:(UIBarButtonItem*)buttonItem
{
NSLog(@"右边按钮点击了");
SecondViewController *second=[[SecondViewController
alloc]
init];
[self.navigationController
pushViewController:second animated:YES];
[second
release];
}
==========================================
popViewControllerAnimated:YES
相当出栈
==========================================
可与直接切换到根视图,中间的视图一次从栈中移除
//返回根视图
[self.navigationController
popToRootViewControllerAnimated:YES];
=======================================================
可以切换到指定的视图
[self.navigationController
popToViewController:self.navigationController.viewControllers[1]
animated:YES];
3.自定义Title
可以通过self.title=@"首页”;设置title
UINavigationController的title可以用别view替代,比如用UIButton
UILable等
UILabel *label=[[UILabel
alloc] initWithFrame:CGRectMake(0,
0, 100,
40)];
label.text=@"首页";
label.font=[UIFont
systemFontOfSize:18];
label.textColor=[UIColor
magentaColor];
label.textAlignment=NSTextAlignmentCenter;
self.navigationItem.titleView=label;
[label
release];
4.显示TooBar
在导航视图控制器中,工具栏默认是隐藏的,使用时先让其显示
[self.navigationController setToolbarHidden:NO animated:YES];
可以在工具栏中添加UIBarButtonItem
//toobarItem属于我们controller
UIBarButtonItem *toolbarItem=[[UIBarButtonItem
alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self
action:@selector(toolbarItemClick:)];
self.toolbarItems=@[toolbarItem];
}
-(void)toolbarItemClick:(UIBarButtonItem*)buttonItem
{
NSLog(@"照相了");
}
相关文章推荐
- Android5.0L因SystemUI ANR导致的黑屏问题分析
- iOS学习笔记(4)UIView和UIImageView
- UIButton/按钮
- uCGUI 按键切换对话框内子控件焦点
- 3000套IOS android源码分享 7.2G UI素材
- 加班~~~第二天之自定义UIPageControl控件
- 【Web探索之旅】第三部分第三课:协议
- UVA 11235--Frequent values+RMQ问题
- cordova build android提示No installed build tools found的解决方法
- UILabel/标签视图
- ActiveX编译报错:Please use the /MD switch for _AFXDLL builds
- ios开发 - UIImageView 的contentMode属性应用
- Uiautomator自动编译运行脚本
- WeifenLuo.WinFormsUI.Docking.dll使用
- 认识UI
- UIView 拖动窗口代码
- UIScrollView无法滚动
- request.getHeader("Referer")中的Referer是从哪里来的?
- UIColor值和颜色的十六进制值(hex值)的互相转换
- jstl 获取map的key 和 value 的值