iPhone开发之导航控制器的预习——UINavigationController导航控制器的学习

 1、导航控制器—— UINavigationController即导航控制器是一个控制器,UINavigationBar相当于一个导航控制器控制的导航条。

2、导航控制器相当于一个视图控制器栈,而UINavigationBar相当于一个存放UINavigationItem的视图栈。导航控制器初始化时以一个跟视图控制器作为rootController。每当导航控制器把一个视图控制器压进栈,就相当于在UINavigationBar栈上覆盖了一层UINavigationItem。后面进栈的UINavigationItem会把前面的给覆盖。

3、一个导航控制器只有一个UINavigationBar栈用来存放所有被压进导航控制器栈的视图控制器所具有的UINavigationItem。每一个被压进导航控制器栈的视图控制器都有各自的一个UINavigationItem。

具体代码如下:

iPhone开发之导航控制器的预习——UINavigationController导航控制器的学习_第1张图片

首先,新建一个空的工程:

编辑AppDelegate.m如下:

//
//  AppDelegate.m
//  导航控制器UINavigation
//
//  Created by apple on 15/9/7.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "AppDelegate.h"

#import "RootViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
       RootViewController *rootVC = [[RootViewController alloc] init];
    UINavigationController *naviCon = [[UINavigationController alloc] initWithRootViewController:rootVC];
    
    self.window .rootViewController = naviCon;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
新建跟视图控制器

编辑RootViewController.h如下:

//
//  RootViewController.h
//  导航控制器UINavigation
//
//  Created by apple on 15/9/7.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController  <UINavigationControllerDelegate>
{
UIButton *btnHid;

}
@end
编辑RootViewController.m如下:
//
//  RootViewController.m
//  导航控制器UINavigation
//
//  Created by apple on 15/9/7.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "RootViewController.h"
#import "secViewController.h"
#import "Header.h"
@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
//    //  设置UINavigationBar的透明度 方法一
//    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    //  设置UINavigationBar的透明度 方法二
    [self.navigationController.navigationBar  setAlpha:0.8];
    
    // 设置导航控制器导航条的背景颜色
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    [ self.navigationItem setTitle:@"第一个"];  //设置导航标题
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 162, 33)];
    img.image = [UIImage imageNamed:@"centerNews"];
    [self.navigationItem setTitleView:img]; // 设置导航标题控件
    // 注意:要么只设置导航标题,不设置导航标题控件。如都进行设置,不论先后只显示标题控件
    
    // 自定义控件
    UIButton * leftbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 29, 29)];
    [leftbtn setBackgroundImage:[UIImage imageNamed:@"个人中心iocn"] forState:UIControlStateNormal];
    UIButton *rightbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 11, 22)];
    [rightbtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [rightbtn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    //将自定义控件转为UIBarButtonItem
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftbtn];
    UIBarButtonItem *rightItem =[[UIBarButtonItem alloc] initWithCustomView:rightbtn];
    // 设置左右按钮
    [self.navigationItem setLeftBarButtonItem:leftItem]; // 设置左方按钮
    [self.navigationItem setRightBarButtonItem:rightItem];

    //添加隐藏导航条功能的按钮
    btnHid = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH/5, WIDTH/4, WIDTH/8, WIDTH/9)];
    [btnHid  setTitle:@"隐藏" forState:UIControlStateNormal];
    btnHid.backgroundColor = [UIColor darkGrayColor];
    [btnHid addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnHid];

    self.navigationController.delegate = self;
  
}

-(void) next
{
    secViewController *secVC = [[secViewController alloc] init];
    
    // 将第二个控制器对象压进栈
    [self.navigationController pushViewController:secVC animated:YES];


}

-(void)hidden
{
    if (!self.navigationController.navigationBar.isHidden) {
        self.navigationController.navigationBar.hidden = YES;
        [btnHid  setTitle:@"展开" forState:UIControlStateNormal];
    }else
    {
        self.navigationController.navigationBar.hidden = NO;
        [btnHid  setTitle:@"隐藏" forState:UIControlStateNormal];
    }

}

-(void)viewWillAppear:(BOOL)animated
{

    [self viewDidLoad];
}

   // 让根视图控制器作为当前导航控制器的Delegate,实现UINavigationControllerDelegate协议中的方法如下
   // 切记只能让跟视图控制器作为当前导航控制器的Delegate或者给导航控制器绑定别的对象做Delegate时只能在跟视图控制器中进行实现
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated   // 因为只有一个导航控制器,所以不需要根据tag值进行判断
{
    
    NSArray *array = navigationController.viewControllers;  // 获取导航控制器栈内的所有视图控制器
    int index = [array indexOfObject:viewController]; // 获取将要跳转的新的视图控制器在导航控制器栈内的位置
    
     NSLog(@"将要跳转 第  %d个控制器",index +1);
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSArray *array = navigationController.viewControllers;  // 获取navigationController导航控制器栈内的所有视图控制器
     int  index = [array indexOfObject:viewController];  // 获取跳转成功后的新的视图控制器在导航控制器栈内的位置
    NSLog(@"跳转成功,显示第 %d个控制器",index +1 );
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
新建要跳转的第二个视图控制器:

编辑secViewController.h如下:

//
//  secViewController.h
//  导航控制器UINavigation
//
//  Created by apple on 15/9/7.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface secViewController : UIViewController

@end
编辑secViewController.m如下:
//
//  secViewController.m
//  导航控制器UINavigation
//
//  Created by apple on 15/9/7.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "secViewController.h"
#import "RootViewController.h"
#import "thirdViewController.h"
@interface secViewController ()

@end

@implementation secViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"第二个";  // 定义标题
    
    
     // 设置背景颜色
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
    
    // 设置标题特性
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor yellowColor],UITextAttributeTextColor, nil];
    
       // 自定义控件
    UIButton * leftbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
    [leftbtn addTarget:self action:@selector(pre) forControlEvents:UIControlEventTouchUpInside];
    UIButton *rightbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
    [rightbtn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    //将自定义控件转为UIBarButtonItem
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftbtn];
    UIBarButtonItem *rightItem =[[UIBarButtonItem alloc] initWithCustomView:rightbtn];
    
    // 设置左右按钮
    [self.navigationItem setLeftBarButtonItem:leftItem]; // 设置左方按钮
    [self.navigationItem setRightBarButtonItem:rightItem];
   

}

-(void) next
{
    // 点击下一页会将第三个控制器对象压进栈
    thirdViewController *thirVC = [[thirdViewController alloc] init];
    [self.navigationController pushViewController:thirVC animated:YES];
}

-(void) pre
{
   // 将当前的视图控制器从导航控制器中出栈移除,直接显示上一个控制器的界面
  // 默认的即系统自带的后退按钮也是将当前的视图从导航控制器中出栈移除
    
    [self.navigationController  popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    
    [self viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
运行结果如下:

iPhone开发之导航控制器的预习——UINavigationController导航控制器的学习_第2张图片iPhone开发之导航控制器的预习——UINavigationController导航控制器的学习_第3张图片iPhone开发之导航控制器的预习——UINavigationController导航控制器的学习_第4张图片




你可能感兴趣的:(iPhone开发之导航控制器的预习——UINavigationController导航控制器的学习)