答案是: 会的
在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];
}
可以看到ViewController
的 dealloc
方法被调用了
- (void)dealloc {
NSLog(@"dealloc: nav:%@ -- self:%@" , self.navigationController , NSStringFromClass([self class]));
}
因为iOS是引用计数管理办法。即当一个对象没有被其他对象引用的时候,它就会被自动释放。
demo: https://github.com/sushushu/Test_RootViewController_Replace
网友评论