Swift /如何正确解除rootViewController?

时间:2016-09-08 05:23:38

标签: ios swift uiviewcontroller

在我的一个视图中,我正在创建一个带有以下内容的rootViewController:

    dispatch_async(dispatch_get_main_queue()) {

        let pageController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
        let nav = RootDJasUserVC(rootViewController: pageController)

        appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)

        appDelegate.window?.rootViewController = nav
        appDelegate.window?.makeKeyAndVisible()

    }

使用 RootDJasUserVC的视图我正在调用另一个函数来访问另一个rootViewController:

    dispatch_async(dispatch_get_main_queue()) {

        let pageController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
        let nav = RootVC(rootViewController: pageController)

        appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)

        appDelegate.window?.rootViewController = nav
        appDelegate.window?.makeKeyAndVisible()

    }

我不想回到RootDJasUserVC,但我希望RootDJasUserVC全新加载。目前,当我回到RootDJasUserVC时(与第一个代码块具有相同的功能),viewDidLoad将不会被执行,因为视图显然仍在某处加载。

如何正确地“解散”这样的rootViewController,让它“重新加载”?非常感谢帮助。

PS。我希望在RootDJasUserVC内完成这样的事情:

dismissViewControllerAnimated(true, completion: nil)

但是当我执行时,就会发生这种情况。

PPS。那些rootViewControllers是UINavigationController

的子类

2 个答案:

答案 0 :(得分:1)

将重新加载代码放在viewWillAppear的{​​{1}}中(RootDJasUserVC仅运行一次),每次返回该视图时都会调用它,我相信这是更好的比创建它的新实例的方式

答案 1 :(得分:1)

每次都不需要更改根视图控制器,如果你想在任何时候你可以呈现视图控制器,让我们拿你的代码,

在下面的代码中你只是将根视图控制器设置为RootDJasUserVC

dispatch_async(dispatch_get_main_queue()) {

    let pageController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
    let nav = RootDJasUserVC(rootViewController: pageController)

    appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    appDelegate.window?.rootViewController = nav
    appDelegate.window?.makeKeyAndVisible()
}

如果你想显示新控制器,则无需更改根视图控制器RootDJasUserVC,只需提供视图控制器,例如,

dispatch_async(dispatch_get_main_queue()) {

    let pageController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
    let nav = RootVC(rootViewController: pageController) //check with simply presenting the view controller if u don't want navigation controller

    //appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    //let rootVc = appDelegate.window?.rootViewController!
    self.presentViewController(nav, animated: false, completion: {

        })
    //no need of below code
    //appDelegate.window?.rootViewController = nav
    //appDelegate.window?.makeKeyAndVisible()
}

一旦你出现,你可以解雇视图控制器

在取消视图控制器后使用dismissViewControllerAnimated(true, completion: nil),调用viewWillAppearviewDidlAppear方法,使用此方法刷新RootDJasUserVC

修改2

我不知道究竟发生了什么,但如果您想加载新的根视图控制器,请执行以下操作,

只需在app delegate中添加一个方法来加载新的根视图控制器

func loadNewRootViewController()
{
    let controller:RootDJasUserVC = RootDJasUserVC(nibName: "RootDJasUserVC", bundle: nil)
    let aNavController:UINavigationController? = UINavigationController(rootViewController: controller)
    self.window?.rootViewController = aNavController;
    self.window?.makeKeyAndVisible()
}

上面的方法加载新的新的根视图控制器,所有你需要调用它一旦视图控制器的解雇让我们说解雇视图控制器后

self.dismissViewControllerAnimated(false) { 
        //change the root hear
       let appdelegate =  UIApplication.sharedApplication().delegate as! AppDelegate
       appdelegate.loadNewRootViewController() //load new fresh root view controller
}

编辑3

 self.navigationController!.dismissViewControllerAnimated(false)  { 
        //change the root hear
       let appdelegate =  UIApplication.sharedApplication().delegate as! AppDelegate
       appdelegate.loadNewRootViewController() //load new fresh root view controller
}

编辑4

同时显示第二个视图控制器, 显示pageController

 let pageController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
 let nav = RootVC(rootViewController: pageController) //check with simply presenting the view controller if u don't want navigation controller

 //appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
 //let rootVc = appDelegate.window?.rootViewController!
 self.presentViewController(nav, animated: false, completion: {

  })

它将显示你所说的新控制器,在这个类中你需要调用dismiss方法,例如,

在呈现视图控制器的第二个中,你可以像下面那样解除

 self.navigationController!.dismissViewControllerAnimated(false)  { 
    //change the root hear
   let appdelegate =  UIApplication.sharedApplication().delegate as! AppDelegate
   appdelegate.loadNewRootViewController() //load new fresh root view controller
 } 

听到self.navigationController不应该是零,因为在展示你设置RootVC()时它是导航控制器。