IOS - presentViewControllerAnimated vs window.rootViewController
I have a class MasterViewController
that is loaded when the application starts. Internally, viewDidLoad()
it checks if the user is logged in and presents one view controller or other result based on the result. If you are not logged in and then proceed with this, the application will download a new one MasterViewController
. My goal is to essentially replace the existing one MasterViewController
with a new instance so that it does validation again viewDidLoad
. I've tried the following and they both work:
// changing the root view controller
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
appDelegate.window!.rootViewController = MasterViewController()
appDelegate.window!.makeKeyAndVisible()
// using presentViewControllerAnimated
someViewController.presentViewControllerAnimated(MasterViewController(), animated: true, completion: nil)
... but while it presentViewControllerAnimated
has nice animation, there is no root change. More importantly, changing the root view controller does not destroy the existing one (at least deinit is never called ..), and obviously presentViewControllerAnimated
does not, so in both cases I have this view controller floating around that I am more I do not want.
I can imagine some kind of scenario where the user logs out and back in and I have 10 MasterViewControllers on top of each other. Any way to completely clear the view controller? Or is it just completely unnecessary?
EDIT
Just remembered presentViewControllerAnimated
for the vc view modally, so definitely not what I want. It would be nice to change the root view controller with similar animation. All the animations I've seen with the root vc changes have been pretty fun.
source to share
The following Stackoverflow thread addresses your concern about the old being UIViewController
used in memory:
fooobar.com/questions/106494 / ...
serge-k's answer from UIWindow Class Reference :
var rootViewController: UIViewController?
Root controller for the window.
The root view controller provides a view of the contents of the window. Assigning a view controller to this property (either programmatically or using the Interface Builder) sets the view of view controllers to be the view of the window's contents. If the window has an existing hierarchy view, the old views are removed before the new one is installed. The default value for this property is nil.
The section Managing Resources in a UIViewController in the UIViewController Programming Guide also contains a note on this subject:
The default behavior for a view controller is to load its view hierarchy when first accessing the view and then saving it in memory until the image controller is removed. The memory used to attract itself to the screen is potentially quite large. However, the system automatically releases these expensive resources when the view is not attached to the window. The remaining memory used by most views is small enough that the system cannot automatically clean up and recreate the view hierarchy.
You can explicitly free the view hierarchy if this additional memory is needed for your application. Listing 4-3 overrides the doReceiveMemoryWarning method to achieve this. First, calls the superclasses to get whatever default behavior you want. Then, it cleans up the view manager resources. Finally, it checks if the view of the view manager is not displayed on the screen. If the view is associated with a window, then it clears any of the view controllers of the view reference and its approaches. If the views store data that needs to be recreated, the implementation of this method must have the data before releasing any of the references to those views.
source to share
Below is the code and concept
var window: UIWindow?
var viewController: MasterViewController?
var navigationVC:UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
window = UIWindow(frame: UIScreen.mainScreen().bounds)
if let window = window {
window.backgroundColor = UIColor.whiteColor()
viewController = MasterViewController(nibName: "MasterViewController", bundle: nil);
//If you want NavigtionController
navigationVC = UINavigationController(rootViewController: viewController!)
navigationVC?.navigationBar.hidden = true
window.rootViewController = navigationVC
//Or If you don't want navigation controller
window.rootViewController = viewController
window.makeKeyAndVisible()
}
return true
}
Here
1.The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controllerโs view as the content view of the window.
2.If the window has an existing view hierarchy, the old views are removed before the new ones are installed.
3.The default value of this property is nil.
source to share