zoukankan      html  css  js  c++  java
  • 源码03-02-10-导航控制器简单使用

    //
    //  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
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    udev文件系统的使用和基本工作原理分析
    ARM平台上蓝牙协议栈Bluez的移植使用和配置
    udev文件系统的使用和基本工作原理分析
    ARM平台上蓝牙协议栈Bluez的移植使用和配置
    Linux 下用户空间与内核空间数据交换的方式
    详解Linux2.6内核中基于platform机制的驱动模型
    IOS怎么判断网络连接
    ios 设备方向判断
    IOS加解密
    用代码向网站提交数据
  • 原文地址:https://www.cnblogs.com/laugh/p/6549963.html
Copyright © 2011-2022 走看看