Swift - Keep the same instantiated UIViewController when switch tabs in TabbarController - swift

I'm writing an app with two or three main features. So I use Tabbarcontroller to switch between viewcontrollers (VC). The problem is, if I switch from VC1 to VC2, and then switch back to VC1, content of VC1 has changed when it moves from background to the foreground.
Does it mean that this is a newly instantiated VC, different from the VC1?
If so, how can I keep VC1 and push it to the front without creating a new VC?
I'm trying to search for the solutions but I don't know which keyword to begin with.
Please help me. Thanks.

The default behavior of UITabBarController is to hold references to the tabs (the ViewControllers) in it´s viewControllers property. If you switch back and forth the viewDidAppear() method is called on the ViewController for the selected tab.
The controllers managed by a UITabBarController will be allocated while the TabController is allocated. After that it will use the same instances of the controllers.
You may update some UI in viewDidAppear?
For reference: UITabBarController

Related

Tab Bar Controller Methods and Properties in other ViewControllers

I have been looking around and I can't find any clear answers. How can I access methods and properties written in the TabBarController from a ViewController attached to it?
The TabBarController has ViewControllers A and B attached to it. VCA has VA1, and VCB has VC2 each connected by a segue. How can I allow VC1 to use methods and properties found on TabBarController.
In this case your tab bar controller can be accessed via parentViewController of child VCs, unless those are inside their own containers, like navigation VC, then you'll need to go up one level.

Genuine UITabBarController and UINavigationController architecture clarity

I have a TabBarController in which I have 5 tabs to access. In one of the tabs I have a Navigation Controller and then the stacks. The navigation controller is being set through IB at this point and the viewcontroller in there is also. Now my concern is that, how can I redraw something based on that particular tabbar as the tableview is not reloading the data since viewWillAppear is not being called when I am in that particular tab.
I am trying to analyze as what is the best way to trigger being in such active tab to trigger such routine. I tried tabbarcontroller didselectviewcontroller delegate pattern in my viewcontroller, and have linked the delegate in my IB from the navigation controller to tabbarcontroller.
However such delegate is not getting triggered when I am in that particular tab item. Any suggestions at this point will be really great?
Thanks

How to switch between two view controllers

I have two UIViewControllers, vc1 and vc2.
I want to switch between them. But before loading the view of the new view controller, I want to destroy/release/remove (I'm not sure abt the correct word to use here) the previous viewcontroller.
For example, when I am switching to vc2 from vc1 ,I want to destroy vc1 completely, so that when I return to vc1 from vc2, vc1 will be loaded from scratch (i.e. viewDidLoad will be executed).
Which type of view switching should I opt for?
presentModal...
addSubview.
I am not using a navigation controller.
Currently I am using the presentModal... method, but when I use dismissModalViewcontroller on the newly presented view controller, it doesn't show up a new instance of the previous view controller. Instead, it shows the already running instance of it.
I want the viewDidLoad method of the previous view controller to run when I dismiss the newly presented view controller.
What exactly needs to happen in viewDidLoad?
You also have viewWillAppear available to you, so it could be that you could move the required functionality there and still use the modal presentation.
See this answer. You can do this with or without animation.
Animate change of view controllers without using navigation controller stack, subviews or modal controllers?

Issue related to state maintenance

I have 10 views with UINavigationController in hierarchy. Now when I want to go back from 10 to 9, and so on with back button, which method should I use?
popviewController
popToViewController
I have set a fixed value for all views. At application launch, I get that value. For that value I initialize rootviewController with use of UINavigationController. And my rootviewController is nothing but the first view and after that on next line, I'm pushing a particular view.
If you're pushing the views onto a UINavigationController, you don't need to explicitly set any method for the back button that the UINavigationController object manages, since it will pop the correct view when tapped. It's all automatic.

How to replace a viewController in the NavigationController Stack?

I have a navigationController, I push a new viewController onto it's stack, I have been trying to figure out a way to switch this ViewController with another viewController from within the first ViewController.
I have 3 almost identical views, depending on the user interaction I would like to be able to switch between these views within the same stack index i.e. without pushing or popping views on the navigationController.
So if view C has index 3 on the navigationController stack and the user taps a certain button in view C the C1 view replaces C on the stack and gets index 3.
I have been through the UINavigation Class and can't find a way around this.
If I popViewController from within the current viewController and the tries to pushViewController… well that can't be done as the viewController gets released when popping it. If I instead try to do pushViewController, well then I gets added on top of my current viewController.
I hope it makes sense and that someone can help me out:)
Thanks
You can assign an NSArray of controllers to the viewControllers property to alter the entire stack instantaneously.