• iOS 深入理解UINavigationController 和 UIViewController 之间的关系


    创建三个类 

    C++代码  收藏代码
    1. BasicViewController : UIViewController   
    2. SecondViewController : UIViewController   
    3. ThirdViewController : UIViewController   



    然后我们在BasicViewController .m文件中push一个viewController: 
    SecondViewController *svc = [SecondViewController new]; 
    [self.navigationController pushViewController:svc animated:true]; 
    在SecondViewController.m文件中pop出viewController: 
    [self.navigationController popViewControllerAnimated:true] 

    问题就来了, 
    push和pop是同一个viewController,那为什么用self.navigationController 就可以知道了 

    其中在AppDelegate.m: 

    C++代码  收藏代码
    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    2. {  
    3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    4.     // Override point for customization after application launch.  
    5.   
    6.     BasicViewController  *basicViewController = [BasicViewController new];//实例化内存后,_parentViewController-->nil  
    7.     UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:basicViewController];  
    8.     // self.window.rootViewController = basicViewController.parentViewController;   
    9.     self.window.rootViewController = navigationController;  
    10.       
    11.     [self.window makeKeyAndVisible];  
    12.     return YES;  
    13. }  



    等价于 

    C++代码  收藏代码
    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    2. {  
    3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    4.     // Override point for customization after application launch.  
    5.   
    6.     BasicViewController  *basicViewController = [BasicViewController new];//实例化内存后,_parentViewController-->nil  
    7.     UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:basicViewController];  
    8.     self.window.rootViewController = basicViewController.parentViewController; //不可以注释上一句,因为上一句是为_parentViewController实例化  
    9.     // self.window.rootViewController = navigationController;  
    10.       
    11.     [self.window makeKeyAndVisible];  
    12.     return YES;  
    13. }  



    分配内存如图 

  • 相关阅读:
    spring aop的@Before,@Around,@After,@AfterReturn,@AfterThrowing的理解
    详解Spring 框架中切入点 pointcut 表达式的常用写法
    Spring计时器StopWatch使用
    Java反射-解析ProceedingJoinPoint的方法参数及参数值
    MySQL 中 datetime 和 timestamp 的区别与选择
    java在注解中绑定方法参数的解决方案
    spring aop的@Before,@Around,@After,@AfterReturn,@AfterThrowing的理解
    LocalDateTime和Date的比较与区别
    idea启动java服务报错OutOfMemoryError: GC overhead limit exceeded解决方法
    java在注解中绑定方法参数的解决方案
  • 原文地址:https://www.cnblogs.com/wangluochong/p/5056882.html
Copyright © 2020-2023  润新知