UI - UINavigationController

<AppDelegate.m>

#import "AppDelegate.h"
#import "FirstViewController.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];
    [self.window makeKeyAndVisible];
    
    
    //创建第一个视图控制器
    FirstViewController *firstVC = [[FirstViewController alloc] init];

    //创建导航视图控制器   并把 firstVC 指定为导航视图控制器的根视图控制器
    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:firstVC];
    
    //将导航视图控制器指定为 window 的根视图控制器
    self.window.rootViewController = navC;
    
    [navC release];
    [firstVC release];
    
    
    
    //设置 navigationBar 是否透明
    //navigationBar在透明情况,与contentView会重合一部分区域。在不透明情况,contentView紧挨在navigationBar的下⾯.且当透明状态的时候,设置背景图片时,其默认的平铺效果就会消失.
//    navC.navigationBar.translucent = YES;
    //设置风格
    navC.navigationBar.barStyle = UIBarStyleBlack;
    //设置 title 颜色
    NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName, nil];
    
    navC.navigationBar.titleTextAttributes = dic;
    //设置 tincolor
    navC.navigationBar.tintColor  = [UIColor purpleColor];
    //设置背景图片
    /* navigationBar竖屏下默认高度44,横屏下默认高度32
     对于 navigationBar 背景图片的问题,对尺寸有严格的要求:
     1.当图片高度小于44或者大于64时,会对 nabigationBar 以及 statusBar 同时附上图片,并且是平铺效果
     2.当图片高度等于44时,只会给 navigationBar 附上图片,不会对 statusBar 做任何修改
     3.当图片高度等于64时,会对 navigaBar 和 statusBar 同时附上图片
     */
    
    [navC.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_64.png"] forBarMetrics:UIBarMetricsDefault];
    
    
    
    
    
    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

<FirstViewController.m>

#import "FirstViewController.h"
#import "SecondViewController.h"


@interface FirstViewController ()

@end

@implementation FirstViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor redColor];
    
    //自定义 navigationBar
    [self customNavigationBar];
    
    
    //布局跳转到secondcontrol页面的 button
//    [self layoutButton];
    
}

-(void)customNavigationBar
{
    //设置 title
    self.navigationItem.title = @"firstVC";
    
    //设置 rightBarButtonItem
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(rightItemAction:)];
    //赋值
    self.navigationItem.rightBarButtonItem = rightItem;

    
}
//点击 rightItem   功能代替下面的 button
-(void)rightItemAction:(UIBarButtonItem *)rightItem
{
    NSLog(@"右边Item");
    //创建下一个视图控制器对象
    SecondViewController *secVC = [[SecondViewController alloc]init];
    //使用导航视图控制器推出下一个视图控制器
    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];
}



//-(void)layoutButton
//{
//    UIButton *bt = [UIButton buttonWithType:UIButtonTypeSystem];
//    bt.frame = CGRectMake(240, 64, 80, 40);
//    bt.backgroundColor = [UIColor greenColor];
//    [bt setTitle:@"Next" forState:UIControlStateNormal];
//    [bt addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:bt];
//}
////button 的点击事件
//-(void)push:(UIButton *)button
//{
//    //创建下一个视图控制器对象
//    SecondViewController *secVC = [[SecondViewController alloc] init];
//    
//    //使用导航视图控制器推出下一个视图控制器
//    [self.navigationController pushViewController:secVC animated:YES];
//    //如果运行点击推出下一个页面是会卡顿一下 ,是因为下一个视图控制器要有一个创建 self.view 的过程,所以在下一个视图控制器中添加任何一个属性就不卡了
//    
//    [secVC release];
//}

//当前视图要出现
-(void)viewWillAppear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"%@",self.navigationController.viewControllers);
}
//将要消失
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"%@",self.navigationController.viewControllers);
}

//当前视图已经消失触发的方法
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"%@",self.navigationController.viewControllers);
}

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


@end

<SecondViewController.m>

#import "SecondViewController.h"
#import "ThirdViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor greenColor];
    
    [self customNavigationBar];
    
//    [self layoutButton];
    
}
-(void)customNavigationBar
{
    //设置 title
    self.navigationItem.title = @"secVC";
    
    
    //自定义rightItem
    
    UIButton *rightBt = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBt.frame = CGRectMake(0, 0, 80, 40);
    //设置 title
    [rightBt setTitle:@"Push" forState:UIControlStateNormal];
    //添加点击事件
    [rightBt addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
    //创建 UIBarButtonItem
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBt];
    //赋值
    self.navigationItem.rightBarButtonItem = rightItem;
   
    [rightItem release];
    
    
    //自定义 leftItem
    UIButton *leftBt = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBt.frame  = CGRectMake(0, 0, 80, 40);
    [leftBt setTitle:@"pop" forState:UIControlStateNormal];
    [leftBt addTarget:self action:@selector(pop:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBt];
    self.navigationItem.leftBarButtonItem = leftItem;
    [leftItem release];

}

//-(void)layoutButton
//{
//    UIButton *bt = [UIButton buttonWithType:UIButtonTypeSystem];
//    bt.frame = CGRectMake(240, 64, 80, 40);
//    bt.backgroundColor = [UIColor yellowColor];
//    [bt setTitle:@"Next" forState:UIControlStateNormal];
//    [bt addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:bt];
//}
//button 的点击事件
-(void)push:(UIButton *)leftBt
{
    //创建下一个视图控制器对象
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    
    //使用导航视图控制器推出下一个视图控制器
    [self.navigationController pushViewController:thirdVC animated:YES];
    [thirdVC release];
}

-(void)pop:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}


//视图已经消失
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"%@",self.navigationController.viewControllers);
}

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

@end

<ThirdViewController.m>

#import "ThirdViewController.h"
#import "FourthViewController.h"
@interface ThirdViewController ()

@end

@implementation ThirdViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    [self customNavigationBar];
    [self layoutButton];
    
}
-(void)customNavigationBar
{
    self.navigationItem.title = @"FourthVC";
}

-(void)layoutButton
{
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeSystem];
    bt.frame = CGRectMake(240, 64, 80, 40);
    bt.backgroundColor = [UIColor blueColor];
    [bt setTitle:@"Next" forState:UIControlStateNormal];
    [bt addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bt];
}
-(void)push:(UIButton *)button
{
    FourthViewController *fourthVC = [[FourthViewController alloc]init];
    
    [self.navigationController pushViewController:fourthVC animated:YES];
    [fourthVC release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

<FourthViewController.m>

#import "FourthViewController.h"

@interface FourthViewController ()

@end

@implementation FourthViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blueColor];
    
    [self customNavigation];
    //布局 button
    [self layoutButton];
    
}
-(void)customNavigation
{
    self.navigationItem.title = @"FourthVC";
}


-(void)layoutButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    button.frame = CGRectMake(0 , 64, 80, 40);
    button.backgroundColor = [UIColor yellowColor];
    [button setTitle:@"Last" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pop:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(void)pop:(UIButton *)button
{
//    //pop 到根视图控制器
//    [self.navigationController popToRootViewControllerAnimated:YES];
//    //pop 到指定的视图控制器
//    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
    //pop 到上一个视图控制器
    [self.navigationController popViewControllerAnimated:YES];
    
}

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

@end


你可能感兴趣的:(导航视图控制器)