美文网首页
关于UIWindow设置rootViewController之后

关于UIWindow设置rootViewController之后

作者: 我的发 | 来源:发表于2020-08-06 17:50 被阅读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

相关文章

网友评论

      本文标题:关于UIWindow设置rootViewController之后

      本文链接:https://www.haomeiwen.com/subject/iwpbzhtx.html