导航控制器:UINavigationController

来源:互联网 发布:excel数据去除空格 编辑:程序博客网 时间:2024/04/28 12:18

    /*

     导航控制器:UINavigationController继承自UIViewController

     视图控制器:UIViewController

     UINavigationController:管理UIViewController

     UIViewController:管理UIView

     UINavigationController-》UIViewController-》 UIView

     */

    RootViewController * root = [[[RootViewControlleralloc] init] autorelease];

    UINavigationController * nav = [[[UINavigationControlleralloc] initWithRootViewController:root]autorelease];

    NSLog(@"nav is %p",nav);

    

//    root.view.backgroundColor = [UIColor grayColor];

    //给视图控制器设置标题

    root.title =@"Settings";

    // 关联UINavigationControllerUIViewController

   /*

     每一个导航控制器 都有一个栈 维护它的所有UIViewController

     而且这个栈当中必须至少有一个视图控制器做为该栈的基栈

     

     栈:先进后出

     UINavigationControllerUINavigation Bar(320*44) + CustomView(ctl.view) + UIToolbar(默认隐藏)

     UINavigation Bar:backBarButtonItem(上一级ctl) + leftBarButtomItem + title(titleView) + rightBarButtonItem

     */


    // 显示工具栏

//    nav.toolbarHidden = NO;

    // 让导航栏不透明

    nav.navigationBar.translucent =NO;

//    NSLog(@"%f",nav.navigationBar.frame.size.height);


    self.window.rootViewController = nav;

    

    return YES;

}


RootViewController.m

#import "ViewController1.h"

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

//    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

//    label.backgroundColor = [UIColor redColor];

//    [self.view addSubview:label];

//    [label release];

    

    NSLog(@"%p",self.navigationController);

    UITapGestureRecognizer * tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(myTap)];

    [self.viewaddGestureRecognizer:tap];

    [taprelease];

}


- (void)myTap

{

 //视图控制器入栈

    ViewController1 * ctl1 = [[ViewController1alloc] init];

//    ctl1.navigationController nil

//    ctl1.view.backgroundColor = [UIColor greenColor];

    [self.navigationControllerpushViewController:ctl1 animated:YES];

    NSLog(@"self %p",self.navigationController);

    NSLog(@"ct1 %p",ctl1.navigationController);

    //    ctl1.navigationController 指向真实的物理空间

//    [self presentViewController:ctl1 animated:YES completion:nil];

    [ctl1release];

}


ViewController1.m

- (void)dealloc

{

    NSLog(@"%@ dealloc",[selfclass]);

    [superdealloc];

}


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    UITapGestureRecognizer * tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(click)];

    [self.viewaddGestureRecognizer:tap];

    [taprelease];

    

    self.view.backgroundColor = [UIColorcolorWithRed:arc4random()%255/255.0green:arc4random()%255/255.0blue:arc4random()%255/255.0alpha:1.0];

    //每次push的视图控制器都会保存在该结构中

    NSLog(@"%@",self.navigationController.viewControllers);

    

   UILabel * label = [[UILabelalloc] initWithFrame:CGRectMake(0,0, 100, 30)];

    label.text = [NSStringstringWithFormat:@"%d",self.navigationController.viewControllers.count];

    label.center =CGPointMake(160,230);

    label.textAlignment =NSTextAlignmentCenter;

    label.backgroundColor = [UIColorclearColor];

    [self.viewaddSubview:label];

    [labelrelease];

}


- (void)click

{

    ViewController1 * ctl1 = [[ViewController1alloc] init];

    [self.navigationControllerpushViewController:ctl1 animated:YES];

    // 不要写漏

    [ctl1release];

}




0 0
原创粉丝点击