如何重置根视图控制器

时间:2017-07-25 12:27:20

标签: ios swift

是否可以重置根视图控制器?复位时我的意思是将其重置为初始状态,以便再次调用viewDidLoad。我正在使用UITabBarController,当我退出时,我想要卸载之前加载的所有标签。

3 个答案:

答案 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之后从头开始生成他的生命周期