iso uinavigationcontrollerdemo2

本文介绍了一个使用UIKit导航控制器的应用示例,展示了如何通过按钮触发视图控制器的推入(pushViewController)和弹出(popViewControllerAnimated),并实现了从根视图到深层视图的导航。
//
//  AppDelegate.m
//  uinavigationcontrollerdemo
//
//  Created by panba on 15-12-27.
//  Copyright (c) 2015年 panba. All rights reserved.
//

#import "AppDelegate.h"
#import "rootViewController.h"
@interface AppDelegate ()
            

@end

@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 *rvc = [[rootViewController alloc]init];
    
    UINavigationController *nvaContorller = [[UINavigationController alloc]initWithRootViewController:rvc];
    self.window.rootViewController = nvaContorller ;
    rvc.navigationController.toolbarHidden = YES;
    
    [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
//
//  ViewController2.m
//  uinavigationcontrollerdemo
//
//  Created by panba on 15-12-28.
//  Copyright (c) 2015年 panba. All rights reserved.
//

#import "ViewController2.h"
#import "ViewController3.h"
@interface ViewController2 ()

@end

@implementation ViewController2

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(10, 100, 300, 50);
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"push到vc3" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *popbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    popbtn.frame = CGRectMake(10, 200, 300, 50);
    popbtn.backgroundColor = [UIColor orangeColor];
    [popbtn setTitle:@"popvc2" forState:UIControlStateNormal];
    [self.view addSubview:popbtn];
    [popbtn addTarget:self action:@selector(popClick:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}
-(void)btnClick:(UIButton *)btn
{
    ViewController3 *vc3 = [[ViewController3 alloc]init];
    [self.navigationController pushViewController:vc3 animated:YES];
    
}

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

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


//
//  rootViewController.m
//  uinavigationcontrollerdemo
//
//  Created by panba on 15-12-27.
//  Copyright (c) 2015年 panba. All rights reserved.
//

#import "rootViewController.h"
#import "ViewController2.h"
@interface rootViewController ()

@end

@implementation rootViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(10, 100, 300, 50);
    btn.backgroundColor = [UIColor blueColor];
    [btn setTitle:@"push到vc2" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    // Do any additional setup after loading the view.
}
-(void)btnClick:(UIButton *)btn
{
    ViewController2 *vc2 = [[ViewController2 alloc]init];
    [self.navigationController pushViewController:vc2 animated:YES];
    
}

- (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
//
//  ViewController3.m
//  uinavigationcontrollerdemo
//
//  Created by panba on 15-12-28.
//  Copyright (c) 2015年 panba. All rights reserved.
//

#import "ViewController3.h"
#import "ViewController4.h"
@interface ViewController3 ()

@end

@implementation ViewController3

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(10, 100, 300, 50);
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"push到vc4" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    // Do any additional setup after loading the view.
}
-(void)btnClick:(UIButton *)btn
{
    ViewController4 *vc4 = [[ViewController4 alloc]init];
    [self.navigationController pushViewController:vc4 animated:YES];
    
}
- (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

//
//  ViewController4.m
//  uinavigationcontrollerdemo
//
//  Created by panba on 15-12-29.
//  Copyright (c) 2015年 panba. All rights reserved.
//

#import "ViewController4.h"
#import "ViewController2.h"
@interface ViewController4 ()

@end

@implementation ViewController4

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(10, 100, 300, 50);
    btn.backgroundColor = [UIColor orangeColor];
    btn.tag  =1;
    [btn setTitle:@"push到root" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *popbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    popbtn.frame = CGRectMake(10, 200, 300, 50);
    popbtn.backgroundColor = [UIColor orangeColor];
    [popbtn setTitle:@"pop到vc2" forState:UIControlStateNormal];
    popbtn.tag = 2;
    [self.view addSubview:popbtn];
    [popbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}
-(void)btnClick:(UIButton *)btn
{
    UIButton *mybtn = (UIButton *)btn;
    if (mybtn.tag ==1) {
        //回到主视图控制器
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    else if(mybtn.tag ==2)
    {
        //跳转到vc2
        NSArray *array = self.navigationController.viewControllers;
        [self.navigationController popToViewController:[array objectAtIndex:1] animated:YES];
    }
}

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y型树杈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值