UI基础-UINavigationController使用3

1.UINavigationController介绍

1.1简介

UINavigationController可以翻译为导航控制器,在iOS里经常用到。

下面的图显示了导航控制器的流程。最左侧是根视图,当用户点击其中的General项时 ,General视图会滑入屏幕;当用户继续点击Auto-Lock项时,Auto-Lock视图将滑入屏幕。相应地,在对象管理上,导航控制器使用了导 航堆栈。根视图控制器在堆栈最底层,接下来入栈的是General视图控制器和Auto-Lock视图控制器。可以调用 pushViewControllerAnimated:方法将视图控制器推入栈顶,也可以调用popViewControllerAnimated:方 法将视图控制器弹出堆栈。

1.2UINavigationController结构组成

看下图,UINavigationController有Navigation bar  ,Navigation View ,Navigation toolbar等组成。

2.UINavigationController简单使用

2.1

  • 新建一个空项目UINavigationControllerDemo
  • 新建一个UIViewController,并在UIViewController.xib中添加一个Button设置名字为Goto SecondView
  • 打开AppDelegate.h,添加属性

#import <UIKit/UIKit.h>

@interface MLKAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong,nonatomic) UINavigationController *navController;

@end

AppDelegate.mdidFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    MLKRootViewController *rootController=[[MLKRootViewController alloc]init];
    rootController.title=@"Root View";

    self.navController=[[UINavigationController alloc]init];
    [self.navController pushViewController:rootController animated:YES];
    //
    [self.window addSubview:self.navController.view];
    //
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

运行

2.2 添加UIBarButtonItem

bar ButtonItem分左右UIBarButtonItem。我们把左右的都添加上去。

在RootViewController.m中添加代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //添加UIBarButtonItem
    UIBarButtonItem *leftButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];
    //
    self.navigationItem.leftBarButtonItem=leftButton;

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

UIBarButtonSystemItemAction的风格,这是系统自带的按钮风格,系统自带的按钮有下面这些

2.3响应UIBarButtonItem的点击事件

//响应UIBarButtonItem事件的实现
-(void)selectLeftAction:(id)sender{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航左按钮" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    [alert show];
}

-(void)selectRightAction:(id)sender{

    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航右按钮" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    [alert show];

}

3.UINavigationController实现页面切换

3.1创建一个新的UIViewController SecondViewController

3.2为RootViewController的Button设置点击方法

-(void)goToSecondView:(id)sender{
    MLKSecondViewController *secondView=[[MLKSecondViewController alloc]init];
    [self.navigationController pushViewController:secondView animated:YES];
    secondView.title=@"Second View";
}

SecondViewController页面

4.在导航栏中使用SegmentedControl

如何在导航栏中实现这种效果呢

这就是SegmentedControl

在SecondViewController.m的viewDidLoad添加如下代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSArray *array = [NSArray arrayWithObjects:@"鸡翅",@"排骨", nil];
    UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];

    segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;

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

}

设置点击事件

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

        }
            break;
        case 1:
        {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了排骨" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alter show];
        }
            break;

        default:
            break;
    }
}

左上角的返回上级View的barButtonitem的名字是上级目录的Title,如果title或者适合做button的名字,怎么办呢?我们可以自己定义

在RootViewController viewDidLoad方法

    //自定义backBarButtonItem
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem=backButton;

5.ToolBar

5.1显示ToolBar

在SecondViewController的viewDidLoad方法中添加下面的代码这样ToolBar就显示出来了

    [self.navigationController  setToolbarHidden:NO animated:YES];

在ToolBar上添加UIBarButtonItem

    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]];

注意:用   [self.navigationController.toolbar setItems:(NSArray *) animated:<#(BOOL)#>]这个方法添加item是不起效果的。下面我动态自己添加Toolbar时,这个才起效果。

5.2动态添加ToolBar

SecondViewController.h文件中添加属性

@property UIToolbar *toolBar;

   //先隐藏ToolBar
    [self.navigationController  setToolbarHidden:YES animated:YES];
    UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(goToThirdView:)];
    self.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:searchButton]];
    [self.view addSubview:_toolBar];

响应UIBarButtonItem的点击事件,跳转到第三个页面去

-(void)goToThirdView:(id)sender{
    MLKThirdViewController *thirdView=[[MLKThirdViewController alloc]init];
    [self.navigationController pushViewController:thirdView animated:YES];
    thirdView.title=@"Third View";
}

时间: 2024-08-10 19:17:38

UI基础-UINavigationController使用3的相关文章

UI基础-UINavigationController使用

UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的APP中(如qq,系统相册等)都有 用到.说是使用详解,其实我只会介绍几个自认为比较重要或者容易放错的地方进行讲解,下面让我们挨个探探究竟: 首先上一张图(来自苹果官方文档): UINavigationController view层级 1.navigationItem 我们都知道navigationI

UI基础-UINavigationController使用 2

UINavigationControlle:导航控制器,是iOS中最常用的多视图控制器之一,它用来管理多个试图控制器 导航控制器可以认为是管理控制器的控制器,主要管理有层级关系的控制器 UINavigationController??????的创建 UINavigationController??????继承与UIViewController,以栈的方式管理所控制的视图控制器,至少要有一个被管理的试图控制器,这个控制器我们称作,导航控制器的根视图控制器 任何继承自 UIViewControlle

swift篇第四期:闭包、UI基础、Protocol

首先来讲下闭包吧,其实闭包跟之前C中的block回调函数类似,但这里只研究了基础的使用,我在下面的两个VC中利用闭包做了通讯传值,也算是比较常用的方法吧,回头有时间我再研究下在项目中的其它应用   let sayHello = {     println("nihao") } sayHello() //定义一个闭包函数,与常规方法不同的是后面有个关键字in哦 let add = { (a: Int, b: Int) -> Int in     return a + b } //调

ios开发-UI基础-超级猜图

[注意]转载时请注明出处博客园-吃唐僧肉的小悟空http://www.cnblogs.com/hukezhu/ 本篇文章介绍一个比较综合的小应用----超级猜图. 功能分析: 根据显示的图片,在下面的待选项按钮中选中正确答案按钮,选中的按钮会显示在正确答案按钮中 答案错误,答案颜色变为红色,分数减小 答案正确,答案颜色变为蓝色,两秒自动跳入下一题,分数增加 点击"下一题"可以进入下一个题目 点击"大图",可以放大显示图片,再次点击图片或者背景,图片缩小至原来大小 点

ios开发-UI基础-应用管理(单纯界面)改进5-使用代理实现监听下载按钮的点击(delegate)

[注意]转载时请注明出处博客园-吃唐僧肉的小悟空http://www.cnblogs.com/hukezhu/ 前几篇文章介绍了一个应用管理的小应用,从最开始的单纯实现功能,一步一步就行改进\封装,上篇文章是使用xib进行了优化,本篇文章使用代理实现监听下载按钮的点击. 在原来的基础上,使用代理的主要思路分析: 首先要新建一个协议 声明协议的要实现的方法(一般为optional) 声明一个遵守该协议的代理的属性 使用代理,通知其代理完成操作 在代理中的实现步骤: 遵守协议 设置代理(一般通过拖线

UI基础控件UIButton

一:UI基础 Button控件 1,简单说明:UIView和UIViewController间的关系 一个应用并不一定要有UIViewController,但是为了管理界面事件(比如按钮点击事件)一般我们在创建应用时要先创建控制器,控制器拥有一个UIView属性. UIView用来展示数据.及用户输入数据.监听事件的触发(比如按钮的touchupinside事件) 而控制可以用来处理这些事件,赋值UIView的管理. 按钮可以展示文字和图片,也就是说按钮具有这些属性. 2,按钮的三种状态 nor

Android UI基础

Android UI概述 Android UI由View和ViewGroup组成. ViewGroup是不可见的,用于组织和排版View和ViewGroup. View用户显示内容,以及响应用户的操作. 可以按照需要安排UI的叠放,不过叠放的层数越少,性能上来说越好. Android UI可以在code中生产,不过更加方便的方式是在Android的XML文件中定义UI. Layouts 通过XML方式实现 可以通过2种方式定义界面结构. 1. 在XML定义视图结构 2. 在运行时动态创建视图结构

转发-UI基础教程 – 原生App切图的那些事儿

UI基础教程 – 原生App切图的那些事儿 转发:http://www.shejidaren.com/app-ui-cut-and-slice.html 移动APP切图是UI设计必须学会的一项技能,切图虽然简单,但还是有很多地方需要注意的,下面由tgideas的 LV主唱大人 跟大家讲讲原生App切图的那些事儿,对UI入门或需了解APP切图的设计师来说会有帮助哦. 如何切图? 了解iphone界面的尺寸 最小的分辨率是320×480,我们把这个尺寸定为基准界面尺寸(baseline),基准尺寸所

iOS开发UI基础—IOS开发中Xcode的一些使用技巧

iOS开发UI基础-IOS开发中Xcode的一些使用技巧 一.快捷键的使用 经常用到的快捷键如下: 新建 shift + cmd + n     新建项目 cmd + n             新建文件 视图 option + cmd + 回车 打开助理编辑器 cmd + 回车           显示主窗口 cmd + 0             导航窗口 option + cmd + 0    工具窗口 在.m & .h之间切换           control + cmd + 上/下