创建 UINavigationController(导航控制器)

在AppDelegate.m中创建

    // 创建一个普通控制器
RootViewController *rootVC = [[RootViewController alloc] init]; // UINavigationController主要用来管理一组控制器(至少有一个),自带 view 可是不使用
UINavigationController *rootNC = [[UINavigationController alloc] initWithRootViewController:rootVC]; // 加入 控件时 是否计算导航条的宽度 默认 YES
rootNC.navigationBar.translucent = NO; // 改变导航条的字体颜色
rootNC.navigationBar.tintColor = [UIColor blueColor]; // 改变导航条总体颜色
rootNC.navigationBar.barTintColor = [UIColor brownColor]; // 改变导航条标题颜色
rootNC.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]}; // 给整个导航条加上背景图片
[rootNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.png"] forBarMetrics:UIBarMetricsDefault]; self.window.rootViewController = rootNC;
[rootVC release];
[rootNC release];

入栈和出栈

在 view 中设置 button 属性,并创建 button

入栈要引入后一个页面的控制器(controller)

@property (nonatomic, retain)UIButton *button;

在 controller 中绑定并实现点击事件

- (void)viewDidLoad {
[super viewDidLoad]; self.title = @"首页"; // 绑定点击事件
[self.rootView.button addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside]; } #pragma mark - button 点击事件
- (void)buttonDidClicked:(UIButton *)sender
{
ScendViewController *secondVC = [[ScendViewController alloc] init];
// 压栈处理 进入下一个页面
[self.navigationController pushViewController:secondVC animated:YES];
}

出栈

#pragma mark - 实现点击事件 出栈
- (void)buttonDidClicked:(UIButton *)sender
{
[self.navigationController popViewControllerAnimated:YES]; }

指定页面跳转

#pragma mark - 通用方法 pop到指定控制器
- (void)button2DidClicked:(UIButton *)sender
{
// pop 到指定控制器
// [self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES]; NSArray *array = self.navigationController.viewControllers; RootViewController *root = nil; for (id VC in array) {
if ([VC isKindOfClass:[RootViewController class]]) {
root = VC;
break;
}
} if (root != nil) {
[self.navigationController popToViewController:root animated:YES];
}
}

跳转到根视图控制器

- (void)buttonDidClicked:(UIButton *)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

自己定义返回按钮

- (void)viewDidLoad {
[super viewDidLoad];
// 定义一个返回按钮
// left and right 都能够
UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"我的返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonItemAction:)]; UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"1.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonItemAction:)]; UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(leftButtonItemAction:)]; // 让系统左(右)边按钮换掉
self.navigationItem.leftBarButtonItem = leftButtonItem; }
#pragma mark - 导航栏左边按钮的点击方法
- (void)leftButtonItemAction:(UIBarButtonItem *)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

UI_UINavigationController的更多相关文章

随机推荐

  1. js form settimeout

    <html><head><meta charset="utf8"><script type="text/javascript&q ...

  2. POJ 3230 DP

    f[i][j]=max(f[i][j],f[i-1][k]-a[k][j]+b[i][j]) i->第i天 j-–>到第j个城市 #include <cstdio> #incl ...

  3. chapter6 数据结构基础之习题 Parentheses Balance

    You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...

  4. java线程中断2

    一个线程在未正常结束之前, 被强制终止是很危险的事情. 因为它可能带来完全预料不到的严重后果. 所以你看到Thread.suspend, Thread.stop等方法都被Deprecated了.那么不 ...

  5. Android_传感器光学

    上一篇写了一个小案例方向传感器,与这光学传感器原理大致类似,但其实代码的主要区别得到的类型不一样在这里我一一列举出来: * Sensor.TYPE_ORIENTATION:方向传感器. * Senso ...

  6. redis-linux

    redis3.0.4 server版本 jedis-2.7.2.jar spring-data-redis-1.6.0.RELEASE.jar commons-pool2-2.3.jar spring ...

  7. SLAM:飞行机器人的参数解析-分类

    在水电站存在的山区,公路运输效率极低,盘山公路绕行消耗大量时间,使用飞行机器人进行运输是合适的选择. 实现一位长辈在山区飞行的愿望,任重而道远 常见飞行机器人的参数解析:解读飞行机器人的基本类型及技术 ...

  8. MxNet : use the MxNet windows versioin

    The MxNet needs  the following thirdparties: 1. lapack complie lapack-3.6.1: download the lapack-3.6 ...

  9. 基于 react-navigation 父子组件的跳转链接

    1.在一个页面中中引入一个组件,但是这个组件是一个小组件,例如是一个cell,单独的每个cell都是需要点击有链接跳转的,这个时候通常直接使用 onPress 的跳转就会不起作用 正确的处理方法是,在 ...

  10. Django F查询Q查询Only与Defel

    F/Q查询 测试表 from django.db import models # Create your models here. class MyCharField(models.Field): d ...