FirstApp,iphone开发学习总结6,Navigation的使用

如图所示,实现的功能:

 为了展示,我创建一个NavView文件,里面包含3个View:

@interface NavView1 : UIViewController{ //代表View1
}
@end

@interface NavView2 : UIViewController { //代表View2
}
@end

@interface NavView3 : UIViewController { //代表View3
}
@end

在NavViewController.m文件中,添加头文件:

#import  " NavView.h
- ( id)init {
    self = [super init];
     if (self) {
        [self setTitle: @" 导航标签 "];        
    }
     return self;
}

在- (void)viewDidLoad中实现:在view添加一个按钮,点击后,push View1。

- ( void)viewDidLoad
{
    UIButton *navViewBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    navViewBtn.frame = CGRectMake( 40.030.0240.030.0);
    [navViewBtn setTitle: @" To View1 " forState:UIControlStateNormal];
    [navViewBtn addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside];

    [[self view] addSubview:navViewBtn];
}

按钮事件实现:

- ( void)pushView:( id)sender
{
    NavView1 *nv1 = [[NavView1 alloc] init];
    [[self navigationController] pushViewController:nv1 animated:YES];
    [nv1 release];
}

点击按钮后,进入View1,即NavView1,此view拥有一个Button,push至View2:

- ( id)init {
    self = [super init];
     if (self) {
        [self setTitle: @" View1 "];
    }
     return self;
}
- ( void)viewDidLoad
{
    UIButton *navViewBtn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    navViewBtn1.frame = CGRectMake( 40.030.0240.030.0);
    [navViewBtn1 setTitle: @" To View2 " forState:UIControlStateNormal];
    [navViewBtn1 addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside];
    
    [[self view] addSubview:navViewBtn1];
}
- ( void)pushView:( id)sender
{
    NavView2 *nv2 = [[NavView2 alloc] init];
    [[self navigationController] pushViewController:nv2 animated:YES];
    [nv2 release];
}

当点击按钮,此时,进入View2。

View2做的事比较多:

1. 进入View3

2. 到达RootView

3. 到达View1

首先,创建3个按钮,并执行事件:

- ( id)init {
    self = [super init];
     if (self) {
        [self setTitle: @" View2 "];
    }
     return self;
}
- ( void)viewDidLoad
{
    UIButton *navViewBtn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    navViewBtn2.frame = CGRectMake( 40.030.0240.030.0);
    [navViewBtn2 setTitle: @" Open View3 " forState:UIControlStateNormal];
    [navViewBtn2 addTarget:self action:@selector(openView:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *navViewBtn21 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    navViewBtn21.frame = CGRectMake( 40.080.0240.030.0);
    [navViewBtn21 setTitle: @" To Root " forState:UIControlStateNormal];
    [navViewBtn21 addTarget:self action:@selector(popView:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *navViewBtn22 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    navViewBtn22.frame = CGRectMake( 40.0130.024030);
    [navViewBtn22 setTitle: @" To View1 " forState:UIControlStateNormal];
    [navViewBtn22 addTarget:self action:@selector(popView1:) forControlEvents:UIControlEventTouchUpInside];
    
    [[self view] addSubview:navViewBtn2];
    [[self view] addSubview:navViewBtn21];
    [[self view] addSubview:navViewBtn22];
}

点击第一个“Open View3”按钮时:

//这里使用presentModalViewController,由下弹出。

//创建UINavigationController的作用是进入此时的View,navigation为空,必须新建一个。

- ( void)openView:( id)sender
{
    NavView3 *nv3 = [[NavView3 alloc] init];
    UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:nv3];
    [self presentModalViewController:nv animated:YES];
    [nv release];
    [nv3 release];
}

点击第二个“To Root”按钮时://直接进入RootView,此处要检查navigationController是否存在。

- ( void)popView:( id)sender
{
    [[self navigationController] popToRootViewControllerAnimated:YES];
}

点击第三个“To View1”按钮时,后推一位:

- ( void)popView1:( id)sender
{
    [[self navigationController] popViewControllerAnimated:YES];
}

 //还有一种– popToViewController:animated:,总体类似,参考

差不多都实现了 ,就剩下如何关闭View3了。NavView3的实现:

- ( id)init {
    self = [super init];
     if (self) {
        [self setTitle: @" View3 "];
    }
     return self;
}

为了美观点,将按钮添加到Navigation的左边并添加一个Label显示此处为View3:

- ( void)viewDidLoad
{
    UILabel *navViewLbl3 = [[UILabel alloc] initWithFrame:CGRectMake( 110.0100.0100.030.0)];
    navViewLbl3.text =  @" View3 Here!! ";
    navViewLbl3.font = [UIFont fontWithName: @"" size: 20];
    [[self view] addSubview:navViewLbl3];
    
    UIBarButtonItem *navViewBtn3 = [[UIBarButtonItem alloc] initWithTitle: @" Close " style:UIBarButtonItemStyleDone target:self action:@selector(closeView:)];
    [self.navigationItem setLeftBarButtonItem:navViewBtn3];
}

事件实现:

- ( void)closeView:( id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}

 

持续完善,求指点。

 

To 6 code download:iOS_FirstApp_6.zip

转载于:https://www.cnblogs.com/maxfong/archive/2012/05/03/2481982.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值