关于UIWindow设置rootViewController之后

2020-08-06  本文已影响0人  我的发




答案是: 会的




在AppDelegate里面设置rootViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}

然后在ViewController里面替换掉window的rootViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    SecondViewController *vc = [SecondViewController new];
    [[UIApplication sharedApplication].keyWindow setRootViewController:vc];
}

可以看到ViewControllerdealloc方法被调用了

- (void)dealloc {
    NSLog(@"dealloc: nav:%@ --  self:%@" , self.navigationController , NSStringFromClass([self class]));
}




因为iOS是引用计数管理办法。即当一个对象没有被其他对象引用的时候,它就会被自动释放。

demo: https://github.com/sushushu/Test_RootViewController_Replace

上一篇 下一篇

猜你喜欢

热点阅读