Calling a view controller class without popping up the view controller itself - ios

I am upgrading my app version, hence needed to reduce two view controllers to one. Hence, I am calling the second view controller skipping the first one. But here I need to call the class of first view controller as there are some important declarations and implementations. How can I call first view controller's class without popping up its view controller.

This is confusing.
Why not taking all the initializing code from your first VC (copy-paste your properties, initialization in viewDidLoad etc.) to your second VC ?
Once you are done, get rid of the first VC as it is useless and make your second view controller the root.
A ViewController is meant to be "viewed", I suggest you don't just hide it, that's a really bad architecture.

If you don't need one view controller at all, then you can delete that View Controller from storyBoard, and also subclass your class as an NSObject class (lets call it DataProviderClass) instead of previously( UIViewController) subClass. It isn't a good idea to have a "dummy" view controller in a navigation Stack.
You can use your DataProviderClass class as a support file that can provide any data to your Second View Controller. And to perform calculations/methods in this class before launching your second VC, just run these methods in viewdidLoad method, by creating an instance of this NSObject class (DataProviderClass) and keeping a reference to it.
When you segue further, you can very easily even transfer the same reference of DataProviderClass.

Related

In iOS, does a segue instantiate the new-to-be-used view controller? Or is it already instantiated?

So for instance, let's say I have a regular subclass of UIViewController and I have connected a control object contained within this controller's view to a segue action that will let another view controller's view come into view...
Simple enough.
When I call the method called prepare(for:sender:) on the regular subclassed UIViewController, at this point, I'm concerned with the new to be used view controller whose view will pop on the screen.. Is this new view controller already instantiated somewhere?
I believe the answer is yes because inside the prepare(for:sender:) function, I set a reference for segue.destination (which is the destination view controller) and when I print that reference, it seems to be a place in memory already which tells me that the new view controller is already instantiated.
Can anyone confirm/deny that this new view controller (created from the storyboard) already has been instantiated, or put this in simpler terms?
Thanks
Apple's documentation says,
When the storyboard runtime detects a custom segue, it creates a new instance of your class, configures it with the view controller objects, asks the view controller source to prepare for the segue, and then performs the segue.
( https://developer.apple.com/reference/uikit/uistoryboardsegue )
So the destination UIViewController is instantiated by the segue just before sending prepareForSegue to the source UIViewController.
So to answer your questions directly, it is "yes" to both questions:
In iOS, does a segue instantiate the new-to-be-used view controller?
Yes, the segue does instantiate the destination view controller.
Or is it already instantiated?
Yes, by the time your prepareForSegue is called, it is already instantiated - immediately beforehand.
UPDATE: As #Jeffery_Thomas commented, this is trivially easy to demonstrate by adding an NSLog() line to your destination view controller's init.
Can anyone confirm/deny that this new view controller (created from the storyboard) already has been instantiated,
Yes. That is what it means to trigger a segue. A triggered segue's job is to instantiate the destination view controller, and prepare exists so that you can configure that instance.

View as SubView VS ChildViewController

Can any one explain when should we add a UIViewController as ChildViewController?
What is the benefits of adding it as ChildViewController instead of subView?
Please help me to understand the purpose of ChildViewController.
When you add a view controller as child view controller, the parent view controller will hold a strong pointer to the child view controller so it doesn't get released instantly. This does not automatically add child's view to parent's view. So you will have to call them both.
I only used it when I needed to create multiple view controllers to be inserted in another view controller and didn't need to directly access it.
its all about UI and code management if you are using subview to achieve what you want to implement inside your app you need to code for your view inside same viewcontrollers class but something interesting i found by creating childviewcontrollers.
empowered to work on a seprate viewcontroller will invoked along with its parent viewcontroller along with its seprate class.
infinite controllers that will be updated tapping a button.
Creation of childViewControllers can be achived by implementing containerView.
or you must have a look of this link hope its helpful to understand.

How can I write and call a designated initializer for my view controller to initialize a private property, using storyboards?

I'd like to write a designated initializer in Swift for my view controller to initialize a private property. I understand how to do this in code, but I'm struggling to understand how to accomplish this with storyboards.
My custom game view controller is the root view controller of my nav controller, which is contained in a container view controller
Since all of the containment and view controller creation is happening automatically from the storyboard, the only solution I can think of is to declare my private property as a public property, so I can assign it a value after my game view controller has been created. This seems like a hack, since I'll only be declaring it as public in order to make use of storyboards. Am I understanding correctly, or is a point in the code when I can intervene and call my game view controller's designated initializer?
Thanks in advance for your wisdom!
Essentially, you can't. As you have found, when you instantiate a view controller from a storyboard, you don't have control over the initialiser that is used.
This is one of the barriers to using a true dependency injection pattern in Cocoa.
The best you can do is use prepareForSegue to set the properties on the newly created view controller.

ViewdidLoad called every time

Why every time I change UIViewController embed in UINavigationController using show push in storyboard ViewDidLoad is called?
It hasn't to be called only once or I have to check programmatically if it is already loaded?
Another relative question:
In the following best practices found here in StackOverflow that user are talking about init method, but if my ViewController are loaded by storyboard where I have to initialise my properties?
Best practices
Remember not to do view controller initialisation in viewDidLoad. This is a common mistake. For stuff that should only happen once when the view controller is loaded, do it in one of the controller's init methods.
viewDidLoad is called first time when the viewController's view is loaded, (either by accessing the view controller's view or by presenting a view controller via modal presentation or via a push presentation). Once the view controller is loaded, viewDidLoad will not get called again. If you want to use init method, you need to use initWithCoder for the things that are from storyboard.

How to pass data between PageViewController and ViewController?

In my app i am using PageViewController which has two different View Controllers. I implemented that it works fine. Now what i am trying is to pass some information from MainView Controller to ChildView Controllers when bar button clicked. I tried using protocol,but it doesn't seems to be working.
MainViewController
import ViewControllerA
import ViewControllerB
#protocol MainControllerDelegate<NSObject>
-(void) passInformation :(NSString*)someInfo;
end
MainController<PageViewControllerDelegate>
In
ViewControllerA
What i would like to see is accessing the main controller delegate in child view controller and pass information when some action taken placed in main view controller navigation bar button.
ViewControllerA<MainControllerDelegate> // Can't find the delegate saying undefined
I am sure there will be a way to pass information between UIPageViewController and its Child View controllers. I tried a lot but couldn't find the answer.
Did you import the MainViewController.h file into your ViewControllerA file?
You haven't provided enough information. Post the whole header for both classes, indicating which one is the parent and which is the child. Post the code in the parent view controller that creates the child view controllers. The trick is when you instantiate a view controller that fills one of the pages in your page view controller, you need to set a property in the child that points the child view controller.
I've been working in Mac OS lately, so I'm a bit rusty on page view controllers. If I remember correctly, you pass them an array of the view controllers they manage.
You'd instantiate each of the child view controllers, set up pointers both ways, and then install them in the page view controller.

Resources