What are the differences between instantiateViewController(withIdentifier:) vs ViewController(nibName:bundle:)? - ios

I'm trying to understand communication between two view controllers.
When passing data without segue, I saw two methods to create destination controller instance.
First one is storyboard?.instantiateViewController(withIdentifier:). Second one is ViewController(nibName:bundle:). Then present them with present(UIViewController..).
What are the differences between the two methods? Are they just to find view controllers by a storyboard identifier or a nibName?

instantiateViewController(withIdentifier) is for view controllers that are inside a storyboard, while UIViewController(nibName:bundle:) is for ones that were created in a .xib file. Both can be used, it just depends on the method the application was created.
If you are planning to make complex view controller relationships, storyboards might be the better solution for you, since view controller organization is more efficient when you can structure your VCs in a two-dimensional place.

Related

How should one cope with multiple delegates in a paged UIScrollView

I have a UIScrollView, contains 3 pages.
The first two are UITableView, the third one is a UICollectionView
So in one View Controller I will have to write delegate methods for all of them.
I googled a bit and found a solution like this (not tested yet),
Place each page in a separate UIViewController, implement the
corresponding delegate methods, and use UIViewController.view
attribute to build the scroll view
The UIViewController.view approach seems wrong to me, is that the normal way to do it?
Why are you trying to handle all the delegate calls in one view controller, can't you create separate view controller for each of them? If you are not separating now then it will be tougher for you to manage code later.
Absolutely using controller composition is a standard and respected solution to the Massive View Controller problem. Examine for instance the expositions at the StackViewController project which cleverly uses that concept to create forms in a UIStackView:
Composition over inheritance is a fundamental principle of object-oriented programming.
This principle has always been used in iOS view hierarchies, where more complex views are composed out of simpler ones (e.g. how a UIButton contains a UILabel and a UIImageView that render its content). However, there was no "official" way to compose view controllers until the introduction of view controller containment in iOS 5. It was possible to mimic behaviour like this prior to iOS 5, but handling the propagation of events between parent and child view controllers and transitions between child view controllers was difficult to get right, which are all problems that the view controller containment API solves.
In the same way that you can create complex layouts by composing multiple UIStackView instances, you can use the view controller containment API to compose multiple instances of StackViewController to create a hierarchy of view controllers where each content view is backed by a corresponding view controller that cleanly separates the responsibilities, instead of handling all of that at the view level (an anti-pattern, as mentioned earlier).
Also review 8 Patterns to Help You Destroy Massive View Controller, particularly
Standard Composition
View controllers can be composed using the View Controller Containment APIs introduced in iOS 5. If your view controller is composed of several logical units that could each be their own view controller, consider using Composition to break them apart. A practical application of this litmus test is a screen with multiple table views or collection views.
Which is, in fact, the exact situation that you have to practically apply it to!

UIStoryboardSegue in MVC-model

I am currently making up a directory structure, which is supposed to enable me to work clearer with the MVC model in my iOS-projects. I therefore created a folder for my Models, Views, Controllers and integrated APIs, but wonder where to put custom segues. I know that they do not belong to the API directory or to the model, but I would neither add them to the Controllers as segues do not coordinate the workflow and not to the Views as they are not visible or contained in a UIViewController. Are there any suggestions where to put the UIStoryboardSegue files? Thanks.
Since Segues are meant to perform transition between controller I would put it with the Controllers.
But note that iOS framework combines view and controller. This approach is called MVVM, hence the name ViewController. This means it will be tricky to treat it as pure MVC.
For instance what would be in the view?
Should it be storyboards, or custom view classes?

Embed segue - switching initial UIViewController and the contained UIViewController dynamically

What I need to do is basically build a container(view controller) that can change its child view controller dynamically and also set it's initial view controller dynamically.
I never used the Embed segue before so I thought I'll give it a shot.
However, using it seems to allow me to change the child view controller dynamically using a custom segue between the children view controllers but the initial view controllers seem to be fixed to the one I dragged the segue to in the StoryBoard(The custom segue here would be something alone these lines).
I know I can achieve what i'm looking for by creating x custom segue (where x is the number of children VCs I need) from the container view controller directly to the children and just calling these segues in code based on my needs.
But if that's the only way, what's the reason for using the "Embed" segue, is it only for really simple scenario's ?
An embed segue is not just for really simple scenarii. It can get pretty complicated. A major purpose is to cleanly separate code related to different concerns, that may still coexist on the same screen, into different view controllers. For instance, you could have an authentication controller and a preferences controller, both embedded into a single profile controller.

View Container vs Multiple ViewControllers vs Hidden Views Single ViewController

I have a View Controller and need to segue into another View Controller (which is customized based on what scenario I want to show). Trying to decide what is the best approach here in regards to simplicity vs efficiency
Three options I can think of:
(a) Have my View Controller segue into a View Controller that holds a View Container linked to multiple View Controllers
(b) Have my View Controller segue into a View Controller with multiple views that can be hidden and rearranged
(c) Have my View Controller segue into different View Controllers depending upon the criteria
I'm still uncertain how much each View Controller should differ from each other, but given that one View Controller might segue into another View Controller that's irrelevant for the other two scenarios, what are your guys' thoughts on the three approaches in terms of code complexity, ease of use, general efficiency in terms of speed/memory management. It's possible that the View Controllers that are being segued into might differ by just a little, but also by a lot!
I think all depends on difference between viewControllers for each scenario. BUT what I can certainly say is do not use varriant (B), because even if difference seems tiny now, as only it begins growing, your code that compose views will becomes more complicated and unreadable, at least in my case it usually happens. So, if you must show different viewControllers with same or pretty similar use (e.g. save for, show list, select item from list. go next, go previouse), try to use one viewController and load different views that are already composed. And if usage of those controllers different - use multiple controllers.

How to change the view controller of the uicontainerview in iOS 6 at runtime

I have a UISegmentedView control with two options. This is part of a MasterViewController. Inside the MasterViewController I have two embedded view controllers, childViewController1 and childViewController2. I have a UIContainerView which is tied to childViewController1. Now I want that when I select the option 2 of the segmented control I should somehow configure the UIContainerView to use the childViewController2.
I'm by no means an expert iOS/Obj-C developer, but I am a bit confused as to what you're trying to accomplish.
What do you mean by change the view controller? You can pass to different view controllers in viewDidLoad. These are wired up in your storyboard, and I think you have to call the navigation controller as well, but I'm not sure.
Either way, I don't understand what you mean by 'segmented' control. Do you have two view controllers? Are you passing data between the two? If so, you'll need to learn about delegates. If not, maybe look at the segue method. My 2 cents.

Resources