您的位置:首页 > 产品设计 > UI/UE

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(@"照相了");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: