navigation controller

一、程序框架

  1、程序结构

  

  2、storyboard

  一个navigation controller 仅保留最基础部分,其他删除

  

   根视图设置为view controller:

   

   

   另外两个视图:

   

二、主要代码

  1、ViewController.m中的主要代码

    1)- (void)viewDidLoad方法

- (void)viewDidLoad {
    [super viewDidLoad];
    //根视图导航栏左侧和右侧的两个按钮
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];
    self.navigationItem.leftBarButtonItem = leftButton;

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(selectRightAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;

    //自定义从下一个视图左上角,“返回”根视图的按钮
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"后退" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem=backButton;

    //自定义根视图下方的工具条,可进一步绑定各个按钮的处理函数;只对当前视图有效,切换到其他视图时为空白
    [self.navigationController  setToolbarHidden:NO animated:YES];
    UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
    UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];
    UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
    UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];

}

    2)导航条左侧按钮响应函数

-(void)selectLeftAction:(id)sender
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏左按钮" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alter show];
}

    3)导航条右侧按钮响应函数

-(void)selectRightAction:(id)sender
{
    VCSecondView *vcSecondView;
    vcSecondView =[self.storyboard instantiateViewControllerWithIdentifier: @"second_view"];
    [self.navigationController pushViewController:vcSecondView animated:YES];
}

  2、VCSecondView.m中的主要代码

    1)- (void)viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    //自定义在导航条显示的对第二个视图对描述;
    //UINavigationController的title可以用别的view替代,比如用UIButton UILable等,此处使用uibutton
    UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    [button setTitle: @"自定义title" forState: UIControlStateNormal];
    [button sizeToFit];
    self.navigationItem.titleView = button;

    //导航栏右侧按钮
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(selectRightAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;

    //自定义从下一个视图左上角,“返回”本视图的按钮
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"后退" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem=backButton;
}

    2)导航条右侧按钮响应函数

-(void)selectRightAction:(id)sender
{
    VCThirdView *vcThirdView;
    vcThirdView =[self.storyboard instantiateViewControllerWithIdentifier: @"third_view"];
    [self.navigationController pushViewController:vcThirdView animated:YES];
}

  2、VCThirdView.m中的主要代码

     均是默认代码

三、运行测试

  1、初始界面

  

  2、单击根视图左侧按钮

  

  3、单击根视图右侧按钮

  

  4、单击第二个视图右侧按钮

  

四、另外两个功能:不常用

  在第二个视图中操作

  1、分割功能:UISegmentedControl

     1)- (void)viewDidLoad添加如下代码: 

- (void)viewDidLoad
{
  。。。
   NSArray *array = [NSArray arrayWithObjects:@"分割a",@"分割b", nil];
    UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];
    segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;

    [segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedController;
  。。。
}

    2)分割按钮响应函数:

-(void)segmentAction:(id)sender
{
     switch ([sender selectedSegmentIndex]) {
         case 0:
         {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了分割a" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alter show];
         }
             break;
         case 1:
         {
             UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了分割b" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
             [alter show];
         }
             break;

         default:
             break;
     }
}

    3)运行结果:

       

      

  2、动态加载自定义的toolbar

    1)VCSecondView.h

#import <UIKit/UIKit.h>

@interface VCSecondView : UIViewController
{
    UIToolbar *toolBar;
}
@end

    2)- (void)viewDidLoad中添加如下代码:

  UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:nil/*@selector(gotoThridView:)*/];
    toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - toolBar.frame.size.height - 44.0, self.view.frame.size.width, 44.0)];
    [toolBar setBarStyle:UIBarStyleDefault];
    toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [toolBar setItems:[NSArray arrayWithObject:addButton]];
    [self.view addSubview:toolBar];

   3)运行结果:切换到第三个视图过程中右上角有黑色阴影

     

五、其他

  导航栏按钮图标类型:

  

  参考:http://blog.csdn.net/totogo2010/article/details/7681879

 

navigation controller

时间: 2024-07-31 03:58:10

navigation controller的相关文章

iOS第八课——Navigation Controller和Tab bar Controller

今天我们要学习Navigation Controller和Tab bar Controller. Navigation Controller是iOS编程中比较常用的一种容器,用来管理多个视图控制器. UINavigationController由Navigation bar ,Navigation View ,Navigation toobar等组成. sicnu_ios_2_os x_swift_3.pdf

从0开始学习Swift开发IOS应用(4)——Navigation Controller

新建一个single view app 在视图窗口拖住一个Navigation Controller,默认的Navigation Controller 是自动关联Table View的,如果不需要,可以把自动添加的Table View 删除 ,新建一个View Controller 关联Navigation Controller即可.(右键从Nav拖向view,选择rootview) 常用属性介绍 navigation bar Style 风格 Translucent 背景,一般把背景去掉 Ba

Navigation Controller 创建方法

? 添加Navigation Controller的方法主要有两种: 第一种:主要是通过在storyboard中拖入Object library 中的Navigation Controller 第二种方法: 选中要添加Navigation Controller的View Controller, 然后点击 "Editor->Embed In->Navigation Controller" ? 就会产生如图所示的Navigation Controller. ? ? ?

storyboard设置navigation controller

到storyboard选中我们唯一一个的viewcontroller,找到xcode的菜单栏,Edit->Embed In->NavigationController.这时候storyboard会自动为你生成一个navigationController,并且讲viewcontroller当做rootViewcontroller. 点击Viewcontroller里的navigationbar.右边面板找到这些属性 找到title 把title改成City.

IOS 改变Navigation的返回按钮

两个办法: 1, 手动为每一个UIViewController添加navigationItem的leftButton的设置代码 2,为UINavigationController实现delegate,在pop和push的时候改变当前和上一页的navigationItem.title 以下是封装的一些基础方法,供参考: + (void) navigationItem:(UINavigationItem*)navigationItem setTitle:(NSString*)title; + (vo

组合View Controller时遇到的一点问题

View Controller的组合应用其实很常见了,比如说Tab bar controller和Navigation view controller的组合使用,像这种一般都是Navigation view controller作为Tab bar controller的一个child view controller,对应了一个Tab bar item. 然后今天在Review 另外一组的一个产品的代码时,发现他们将Tab bar controller作为了一个普通view controller的

从零开始学ios开发(十四):Navigation Controllers and Table Views(上)

这一篇我们将学习一个新的控件Navigation Controller,很多时候Navigation Controller是和Table View紧密结合在一起的,因此在学习Navigation Controller的同时,我们还将继续学习Table View其他一些特性,毕竟Navigation Controller还是相对来说毕竟简单的,没有什么太大的花头,它的主要作用就是一个view的切换,切来切去,而Table View的花头就比较多了,这次我们将这2个控件结合在一起进行学习. 再多说一

Swift - iOS中各种视图控制器(View Controller)的介绍

在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来作为子类,以向屏幕中添加逻辑. 2,导航控制器 - Navigation Controller 这个控制器呈现一个视图控制器的栈,应用程序可以在上面推入更多的视图控制器. 当新视图推入栈,或旧视图弹出栈时,导航控制器会以动画的形式(比如卷动)显示隐藏这些视图. 使用样例:系统“设置”应用程序 3,表

【IOS笔记】View Controller Basics

View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen space for displaying content and therefore must be creative in how they present information to the user. Apps that have lots of information to display