-1

Initially my app starts with a view controller(TabBarView) which I have set in AppDelegate file.

     window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        let tabBarView = TabBarView()
navigationController = UINavigationController(rootViewController: tabBarView)
        window?.rootViewController = navigationController

Later in an app, when few criteria are met, I want to change my rootController to a new one(MusicPage).

At the moment I've create a new NavigationController object to display my (MusicPage)viewController however I think that my (TabBarView)viewController might be running in background and consuming memory.

I would really appreciate if someone tells me how to change my initial rootViewController.

Thanks. :)

Pratik
  • 676
  • 10
  • 9

1 Answers1

1

The answer can be found on

Set rootViewController of UINavigationController by method other than initWithRootViewController

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav

Edited: SWIFT 4

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let yourVc = UIViewController() // your view controller 
appDelegate.window?.rootViewController = yourVc
appDelegate.window?.makeKeyAndVisible()
Harsh
  • 2,852
  • 1
  • 13
  • 27
  • 1
    Link only plus mere plagiarized code is not an answer. You are right to point to the question of which this one is a duplicate, but your duty then is to comment that it’s a duplicate. Don’t try to capitalize on someone else’s answer. – matt Mar 16 '18 at 02:10
  • I gave the source of my answer.. that should not be plagiarism... The only reason i wrote those for lines was that the person who asked the question would not have to navigate to the original answer and search for the one he/she is interested in.. – Harsh Mar 16 '18 at 02:15
  • OK "plagiarism" is not the right word, granted. Sorry. But this is in effect a link-only answer despite the code, is my point. And as such, I've flagged it as not an answer. Again, thanks for pointing out the duplication. – matt Mar 16 '18 at 02:18
  • Cool.. this should have been your first comment as a moderator and just not lash out.. I'll take care in future to mark questions as duplicate and just comment on question if needed .. Thanks for letting me know what the correct process is. – Harsh Mar 16 '18 at 02:22
  • It's not moderation (I have no such rank or power), just an opinion. And my view that it's not really an answer is also just an opinion. Stack Overflow is mostly a "wisdom of crowds" thing; it takes multiple people agreeing to make something happen. Again, sorry for not being clear. – matt Mar 16 '18 at 03:48
  • hey @harsh this code doesn't set new rootViewController, it merely creates a new object of AppDelegate and stacks another window on previous window. Could you please tell me if there is any way to change the original rootViewController ? – Pratik Mar 22 '18 at 02:19
  • @Pratik I edited my answer to include an example for Swift 4. And you are wrong, it does create a new object but of an existing instance from `UIApplication` instance. When you set the `rootViewController` on the window, it replaces the existing one. – Harsh Mar 22 '18 at 02:25
  • https://imgur.com/5gy9yoX This made me believe that it is stacking new window. Thanks for your answer. – Pratik Mar 22 '18 at 02:42
  • I think the rootViewController setting is not the problem. Perhaps you have a retain cycle in your other view controllers that stops them from being deallocated. There are many ways you could accidentally do this (capturing strong references to self in blocks, not marking delegates or other back references as weak, etc). You might be able to figure it out with Instruments. Here's a tutorial: http://samwize.com/2016/05/30/finding-retain-cycle-with-instruments/ – Harsh Mar 22 '18 at 03:34
  • May be something like this is happening. https://stackoverflow.com/questions/26763020/leaking-views-when-changing-rootviewcontroller-inside-transitionwithview – Harsh Mar 22 '18 at 03:35