是否可以重置根视图控制器?复位时我的意思是将其重置为初始状态,以便再次调用viewDidLoad
。我正在使用UITabBarController
,当我退出时,我想要卸载之前加载的所有标签。
答案 0 :(得分:5)
您可以通过在注销操作中将TabBarController的实例设置为 rootViewController 来执行此操作。
斯威夫特3:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyBoard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = tabBarController
UIApplication.shared.keyWindow?.makeKeyAndVisible()
目标C:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabBarController = [storyBoard instantiateViewControllerWithIdentifier:@"TabBarController"];
[[[UIApplication sharedApplication] keyWindow] setRootViewController:tabBarController];
[[[UIApplication sharedApplication] keyWindow] makeKeyAndVisible];
答案 1 :(得分:1)
如果您在Tabbarcontroller
上使用导航控制器,请导航至该导航控制器,否则请转至Tabbarcontroller
-
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabBar = mainStoryboard.instantiateViewControllerWithIdentifier("TabBarController") as! TabBarController
appDelegate.window?.rootViewController = tabBar
appDelegate.window?.makeKeyAndVisible()
答案 2 :(得分:0)
将view
的{{1}}属性设为UIViewController
nil
它会强制UIApplication.shared.keyWindow?.rootViewController?.view = nil
在下次调用UIViewController
之后从头开始生成他的生命周期