写了很长的NavigationController介绍,结果被cnblog吞了,没存档,算了,简单粗暴,直接上如何使用。

  1.创建3个Controller,继承自UIViewController

在AppDelegate.h中

//
// AppDelegate.m
// UINavigationController
//
// Created by lcd on 15/9/11.
// Copyright (c) 2015年 lcd. All rights reserved.
// #import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

FirstViewController *firstViewController = [[FirstViewController alloc] init];


firstViewController.view.backgroundColor = [UIColor redColor];

SecondViewController *secondViewController = [[SecondViewController alloc] init];

secondViewController.view.backgroundColor = [UIColor whiteColor];

ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];

   thirdViewController.view.backgroundColor = [UIColor cyanColor];

//创建导航控制器
UINavigationController *navigation = [[UINavigationController alloc] init]; return YES;
}

2.使用NavigationController有多种方式

//第一种:将需要NavigationController 管理的视图添加进NavigationController 的子视图数组中
UINavigationController *navigation = [[UINavigationController alloc] init];
navigation.viewControllers = @[firstViewController,secondViewController,thirdViewController];
//第二种:逐个添加子控制器
[navigation addChildViewController:firstViewController];
[navigation addChildViewController:secondViewController];
[navigation addChildViewController:thirdViewController];
//第三种:初始化NavigationController时添加自控制器
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:firstViewController];

3.添加子控制器后,将navigation设置为窗口的根控制器

self.window.rootViewController = navigation;

这样运行,显示的是第三个ViewController,这是因为,NavigationController是以栈的方式管理子视图,如图:

先添加的视图被放在了栈底,显示在我们面前的是栈顶视图。点击back会把栈顶视图弹出,即为出栈(pop)操作。

NavigationController以 push(入栈)和pop(出栈)管理子视图。

4. NavigationController 结构

4.1导航栏

被NavigationController管理的视图公用一个导航栏,导航栏显示内容由栈顶控制器决定

在ThirdViewController.m中

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"我是第三个控制器";
}

左上角返回键文字(Back) 由上一个控制器控制

在SecondViewController.m中

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回第二个界面" style:nil target:nil action:nil];
}

运行结果如下图:

4.2 在导航栏添加按钮

UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];

UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];

self.navigationItem.rightBarButtonItems = @[button1, button2];

修改右按钮格式

self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
//左按钮同理

隐藏back按钮

[self.navigationItem setHidesBackButton:YES];

4.3 设置导航条样式

[self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
//样式枚举值如下:

typedef NS_ENUM(NSInteger, UIBarStyle) {

UIBarStyleDefault          = 0,

UIBarStyleBlack            = 1,

UIBarStyleBlackOpaque      = 1, // Deprecated. Use UIBarStyleBlack

UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES

};

设置导航栏图片

[self.navigationController.navigationBar
setBackgroundImage:[UIImage imageNamed:@"图片名称"] forBarMetrics:UIBarMetricsDefault];

4.4 其他属性

//设置导航条颜色
self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
//关闭导航条毛玻璃效果
// self.navigationController.navigationBar.translucent = NO;
//隐藏导航条
// self.navigationController.navigationBarHidden = YES;
//设置导航条内容的颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//导航条背景图片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"pic"] forBarMetrics:UIBarMetricsDefault];

titleTextAttributes

//这是UINavigationBar的一个属性,通过它你可以设置title部分的字体,这个属性定义如下
@property(nonatomic,copy) NSDictionary *titleTextAttributes __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;
//设置title的字体颜色为黄色
NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:UITextAttributeTextColor];
firstViewController.navigationController.navigationBar.titleTextAttributes = dict;

5.界面跳转

从FirstViewController跳转到SceondViewController

在FirstViewController.m中,添加button,并在button事件中添加如下代码:

 SecondViewController *secondViewController = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];

从SceondViewController返回FirstViewController:

[self.navigationController popViewControllerAnimated:YES];

注意:从后一个界面跳转回前一个界面,一定要用popViewController,千万不要alloc一个FirstViewController,push到FirstViewController

[self.navigationController popToRootViewControllerAnimated:YES];//这个方法可以跳转回第一个界面

UINavigationController基本使用的更多相关文章

  1. iOS UINavigationController(内容根据iOS编程编写)

    我们知道 UITabBarController 对象,可以通过使用该对象,用户可以切换不同的屏幕.当要切换的各个屏幕之间没有相互依存关系的时候,该对象可以很好的完成任务.但是当多个屏幕互有关系的时候, ...

  2. UINavigationController

    知识点: 1)UINavigationController 2)UINavigationBar 3)UINavigationItem 4)UIToolBar ===================== ...

  3. 混合使用UITabBarController和UINavigationController

    混合使用这两个控件的好处是我们可以在NavigationBar添加更多的东西,如标题,按钮等.让用户能够获得更多的信息. UITabBarController的属性ViewControllers接受以 ...

  4. 基本组件的使用——UINavigationController

    作用:在多个ViewController中切换.UINavigationController内部以栈的形式维护一组ViewController, 因此,当导航进入一个新视图的时候,会以push的形式将 ...

  5. 解决UINavigationController在pushViewController时出现的"卡顿"问题

    进行开发中,遇到了个小问题: 在使用UINavigationController的-pushViewController:animated:执行入栈一个子控制器操作时(即最新栈顶子控制器),会出现推出 ...

  6. UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解

    (1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最下面,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...

  7. IOS 学习 开发 自定义 UINavigationController 导航

    文件目录如下:基本导航顺序: root -> First -> Second -> Third.其中,FirstViewController作为 navigation堆栈的rootv ...

  8. APP标配控制器:UINavigationController

    导航控制器UINavigationController简介: 只要看到控制器界面上部有一个条就是导航控制器UINavigationController 导航控制器最上面有一个条是导航条高度44,Y值是 ...

  9. UIScrollerView遇到UINavigationController

    今天在UITabBarController  的第一个Tab 页面中放入一个ScrollView, 原本以为可以正常运行. 结果却让人大跌眼镜.  每当我手动滚动或者 缓慢导航到另外一个页面时,当前的 ...

  10. IOS开发之控件篇UINavigationController第一章 - 介绍

    UINavigationController是一个比较常见的控件,它连接个视图,例如一个视图走到另外一个视图,之间的联系都可以用这个NavigationController的方法 一般都会由两个部分组 ...

随机推荐

  1. windows 32位系统中进程最大可用内存空间为3GB (转)

    http://msdn.microsoft.com/zh-cn/library/ms189334.aspx 进程地址空间 所有 32 位应用程序都有 4 GB 的进程地址空间(32 位地址最多可以映射 ...

  2. TP的一条sql语句(子查询)

    $model=M(''); $model->table(C('DB_PREFIX').'goods as g') ->join(C('DB_PREFIX').'orders as o on ...

  3. HTML5 修改浏览器url而不刷新页面

    嘛,起因是黑子大叔在微博上的一条@信息,找起了这个的实现,看了一圈google的中文信息内似乎还没有怎么提到这个的内容,就发表上来. 详细效果就是类似于用Firefox4+/Chrome 5+/Saf ...

  4. [Oracle] - 性能优化工具(2) - ASH

    ASH和AWR的关系 ASH以V$SESSION为基础,每秒採样一次,记录活动会话等待的事件.不活动的会话不会採样,採样工作由新引入的后台进程MMNL来完毕. ASH内存记录数据始终是有限的,为了保存 ...

  5. cocos2dx-jsb 跨语言调用及第三方集成 - 过程记录

    1:C++中调用js方法: 问题:ios中当用户通过home键将游戏转入后台时,调用js中的暂停游戏方法: AppDelegate::applicationDidEnterBackground() 和 ...

  6. MySQL通用优化 叶金荣!!!

    http://mp.weixin.qq.com/s?__biz=MjM5NDE0MjI4MA==&mid=208777870&idx=1&sn=6efddd6283e4deb3 ...

  7. Ildasm.exe 概要:

    一.前言: 微软的IL反编译实用程序——Ildasm.exe,可以对可执行文件(ex,经典的控制台Hello World 的 exe 可执行文件)抽取出 IL 代码,并且给出命名空间以及类的视图.在讲 ...

  8. SQL优化 csdn

    1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起这一问题原因可以很多,有可能是代码不够高效.有可能是硬件或网络问题,也有可能是数据库设计的问题. 本篇博文将针对一些常用的数据库性 ...

  9. 项目源码--Android新闻财经类商用要求源码

    技术要点: 1. 商用要求源码框架 2. 登录与注册用户系统模块 3. Android的Http通信以及多线程处理模块 4. Andorid的网络数据处理模块 5. Andorid的版本检测与升级模块 ...

  10. 关于c#调用c/c++ dll遇到的问题总结

    前段时间公司做了个winform程序,需要调用c 的dll去读取卡号的程序,期间遇到些问题,下面来分享下 一.dll路径问题 相信很多开发者都会遇到这个问题,我总结了下我现在有3总方式去解决这个问题: ...