iOS开发:UINavigationController导航控制器

1、UINavigationController导航控制器如何使用

下 面的图显示了导航控制器的流程。最左侧是根视图,当用户点击其中的General项时 ,会跳转到General视图;接着点击Auto-Lock,又会跳转到另一个界面;当点击左上角的Genderal时又会返回到上一页面。实际上这是入栈出栈的操作, 界面的跳转可以调用 pushViewControllerAnimated:方法将视图控制器推入栈顶,也可以调用popViewControllerAnimated:方 法将视图控制器弹出堆栈。

上图来自苹果官网。

2、UINavigationController的结构组成

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


//开始具体实现:
1.新建工程,关闭ARC(Automatic Reference Counting),即开启手动引用计数;
2.在AppDelegate.h文件中,初始化window,并添加根视图

- (void)dealloc{
 [_window release];

    [super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

       self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];//不添加背景颜色的话,测试时屏幕会卡一下

    [self.window makeKeyAndVisible];

//创建一个视图控制器对象

    ViewController *vc = [[ViewController alloc]init];

//创建一个UINavigationController的对象,即导航控制器

    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];

//设置window的根视图控制器

    _window.rootViewController = nav;

   [nav release];

    [vc  release];

3.在ViewControll.m文件中:
     //相关属性

         //1)是否透明:NO为不透明。

    self.navigationController.navigationBar.translucent = NO;

    //主要区别:不透明的在Navigation View里添加label、button等时以原有window的(0, 128)为原点作参考。如果是透明的,有可能添加的东西被覆盖掉。

如下图:





//下图是不透明的,即self.navigationController.navigationBar.translucent = NO;


   //2)设置导航栏的背景

            //(a)在AppDeglate.m

    //修改状态栏的背景颜色(系统方法)

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

            //(b)在ViewController.m

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.24 green:0.78 blue:0.35 alpha:1];


   //3)修改Navigation背景图

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_64@2x.jpg"] forBarMetrics:UIBarMetricsDefault];







  //4)添加title

    self.title = @"第一页";

            //添加一个SegmentedControl

    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"消息", @"通话", nil]];

    [self.view addSubview:segment];

    //前面这一步只是将segment添加到Navigation View里面了。

//加上下面这一句才是添加到状态栏上

    self.navigationItem.titleView = segment;

     

//左右添加按钮

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-locationfill.png"] style:UIBarButtonItemStylePlain target:self action:@selector(sayHi)];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(sayHi)];

    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"Zapfino" size:18], NSFontAttributeName, nil] forState:UIControlStateNormal];





//跳转页面:1.从前往后跳转:当点击next时触发某个事件,跳转到对应的界面;入栈

(1)、引入要跳转到哪一个的头文件,并初始化一个对象(例如第一页跳转到第二页)

       #import "SecondViewController.h"

- (void)tapAction{

        //初始化对象

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

  //跳转的时候可以加模态视图,效果比较炫

    second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

(2)、跳转

    [self.navigationController pushViewController:second animated:YES];

}

     2. 从后往前跳转:
       a.一般点击左侧自带按钮都会返回到主界面
       b.返回上一页:出栈

       [self.navigationController popViewControllerAnimated:YES];

       c.从某一页跳到前面的中间某一页(例如从第四页直接跳转到第二页):

         (1)、获取所有页面组成的数组

 NSArray *array = self.navigationController.viewControllers;

          (2)、跳转

 [self.navigationController popToViewController:[array objectAtIndex:1] animated:YES];

*说明:[array objectAtIndex:1]是要跳到哪个界面的下标。

     




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值