码迷,mamicode.com
首页 > 其他好文 > 详细

源码03-02-10-导航控制器简单使用

时间:2017-03-14 19:32:40      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:nat   http   view   就会   color   yellow   mem   source   images   

 

技术分享

//
//  AppDelegate.m
//  10-导航控制器简单使用
#import "AppDelegate.h"
#import "OneViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 创建导航控制器的跟控制器,也属于导航控制器的子控制器
    UIViewController *vc = [[OneViewController alloc] init];
    vc.view.backgroundColor = [UIColor redColor];
    
    
    // 导航控制器也需要一个根控制器
    // 默认导航控制器把根控制器的view添加到导航控制器的view上
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
    
    NSLog(@"%@",navVc);
    // 设置窗口的跟控制器
    self.window.rootViewController = navVc;
    
    [self.window makeKeyAndVisible];
    return YES;
}@end

OneViewController.xib

技术分享

//
//  OneViewController.h
//  10-导航控制器简单使用
#import <UIKit/UIKit.h>

@interface OneViewController : UIViewController

@end

 

//
//  OneViewController.m
//  10-导航控制器简单使用
#import "OneViewController.h"
#import "TwoViewController.h"

@interface OneViewController ()

@end

@implementation OneViewController
// 跳转第二个控制器
- (IBAction)jump2Two:(id)sender {
    //在这里设置要跳去的的控制器
    TwoViewController *vc = [[TwoViewController alloc] init];
    
    vc.view.backgroundColor = [UIColor yellowColor];
    
    
    // 跳转
    // 如果导航控制器调用push,就会把vc添加为导航控制器的子控制器
    [self.navigationController pushViewController:vc animated:YES];
    
//    NSLog(@"%@",self.navigationController);
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

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


@end

 

TwoViewCoutroller.xib

技术分享

 

//
//  TwoViewController.m
//  10-导航控制器简单使用
#import "TwoViewController.h"
#import "ThreeViewController.h"

@interface TwoViewController ()

@end

@implementation TwoViewController
- (IBAction)jump2Three:(id)sender {
    ThreeViewController *three = [[ThreeViewController alloc] init];
    
    [self.navigationController pushViewController:three animated:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

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

@end

 ThreeViewController.xib

技术分享

//
//  ThreeViewController.m
//  10-导航控制器简单使用
#import "ThreeViewController.h"

@interface ThreeViewController ()

@end

@implementation ThreeViewController
// 返回上一个控制器
- (IBAction)backToPre:(id)sender {
    
    // pop不是马上把控制器销毁,
    // 回到上一个界面
    [self.navigationController popViewControllerAnimated:YES];
    
}

// 返回到导航控制器的跟控制器
- (IBAction)back2Root:(id)sender {
    

    // 注意:只能返回到栈里面的控制器
    [self.navigationController popToViewController:self.navigationController.childViewControllers[0] animated:YES];
//    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)dealloc
{
    NSLog(@"%s",__func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    NSLog(@"%@",self.navigationController.viewControllers);
    
    NSLog(@"%@",self.navigationController.topViewController);
}

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

 

源码03-02-10-导航控制器简单使用

标签:nat   http   view   就会   color   yellow   mem   source   images   

原文地址:http://www.cnblogs.com/laugh/p/6549963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!