导航控制器的学习

//
//  AppDelegate.m
//  NavDemo
//
//  Created by LQ on 12-9-26.
//  Copyright (c) 2012年 Vistor. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "SubViewController.h"
#import "ThrViewController.h"
@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    MainViewController *mvc=[[MainViewController alloc]init];
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:mvc];
   
    self.window.backgroundColor = [UIColor whiteColor];
    [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

//
//  MainViewController.m
//  NavDemo
//
//  Created by LQ on 9/26/12.
//  Copyright (c) 2012 Visitor. All rights reserved.
//

#import "MainViewController.h"
#import "SubViewController.h"
@interface MainViewController ()

@end

@implementation MainViewController

- (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.navigationItem.title = @"MainView";
    self.view.backgroundColor = [UIColor purpleColor];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(100, 100, 120, 30);
    btn1.tag = 1;
    //按钮点击时显示颜色
    [btn1 setTintColor:[UIColor redColor]];
    //设置按钮上显示的文字的字体
    btn1.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [btn1 setTitle:@"翻转窗体" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
   
   
    //导航控制器上的按钮
    //采用系统提供的导航控制器按钮样式
    UIBarButtonItem *rightBBI = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(bbiClick:)];
    rightBBI.tag = 1;
    //设置导航控制器右侧按钮为自定义的rightBBI按钮
    self.navigationItem.rightBarButtonItem = rightBBI;
   
    //采用自定义导航控制器按钮
    UIBarButtonItem *leftBBI1 = [[UIBarButtonItem alloc]initWithTitle:@"ToSubView1" style:UIBarButtonItemStyleDone target:self action:@selector(bbiClick:)];
    leftBBI1.tag = 2;
   
    UIBarButtonItem *leftBBI2 = [[UIBarButtonItem alloc]initWithTitle:@"ToSubView2" style:UIBarButtonItemStylePlain target:self action:@selector(bbiClick:)];
    leftBBI2.tag = 3;
   
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:leftBBI1,leftBBI2, nil];
   
   
   
}

- (void)bbiClick:(UIBarButtonItem *)bbi
{
    SubViewController *svc = [[SubViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:svc];
    if(bbi.tag == 1)
    {
        //CoverVertical为从下向上弹出新的导航控制器
        nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self.navigationController presentViewController:nav animated:YES completion:^{
            NSLog(@"翻转完成");
        }];
    }
    else if(bbi.tag == 2)
    {
        //CrossDissolve为渐变模式弹出新的导航控制器
        nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self.navigationController presentViewController:nav animated:YES completion:^{
            NSLog(@"翻转完成");
        }];
    }
    else
    {
        //PartialCurl为翻书模式弹出新的导航控制器
        nav.modalTransitionStyle = UIModalTransitionStylePartialCurl;
        [self.navigationController presentViewController:nav animated:YES completion:^{
            NSLog(@"翻转完成");
        }];
    }
}

- (void)btnClick:(UIButton *)btn
{
    /*
     如果使用模式对话窗体3步骤
     1-创建一个新的导航控制器并把主视图控制器设置好
     2-给新的导航控制器设置跳转模式
     3-用当前导航控制器present跳转新的导航控制器
    */
    SubViewController *svc = [[SubViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:svc];
    if(btn.tag == 1)
    {
        //FlipHorizontal为水平翻转弹出新的导航控制器
        nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self.navigationController presentViewController:nav animated:YES completion:^{
            NSLog(@"翻转完成");
        }];
    }
}

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

@end

 //
//  SubViewController.m
//  NavDemo
//
//  Created by LQ  on 9/26/12.
//  Copyright (c) 2012 Visitor. All rights reserved.
//

#import "SubViewController.h"
#import "ThrViewController.h"
@interface SubViewController ()

@end

@implementation SubViewController

- (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.navigationItem.title = @"SubView";
    self.view.backgroundColor = [UIColor greenColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
   
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(100, 100, 120, 30);
    [btn1 setTitle:@"ToThrView" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn1.tag = 1;
    [self.view addSubview:btn1];
   
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame = CGRectMake(100, 150, 120, 30);
    [btn2 setTitle:@"ToMainView" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn2.tag = 2;
    [self.view addSubview:btn2];
   
}

- (void)btnClick:(UIButton *)btn
{
    if(btn.tag == 1)
    {
        ThrViewController *tvc = [[ThrViewController alloc]init];
        [self.navigationController pushViewController:tvc animated:YES];
    }
    else
    {
        //放弃当前模式对话窗体(返回)
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            NSLog(@"放弃当前模式对话窗体");
        }];
    }
}

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

@end

//
//  ThrViewController.m
//  NavDemo
//
//  Created by LQ on 9/26/12.
//  Copyright (c) 2012 Visitor. All rights reserved.
//

#import "ThrViewController.h"

@interface ThrViewController ()

@end

@implementation ThrViewController

- (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.
}

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

@end

 

 

 

 


 

 

 

 

 

 

 


 

 

转载于:https://my.oschina.net/u/811475/blog/80400

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值