How to embed one nib into another? - iphone

There are many view that share a common part in my current project so I just visualize a way to encapsulate the common part into a nib file that could be shared by many controllers by embedding it to their corresponding nib file. Although I know how to do it programmatically, I still believe there should be a way to achieve this simply in Interface Builder. Are there anyone who has achieved this and would you like to point the way out?

You can add in your NIB file a view called "Content", for example. In your code, the only thing you need to do is add your new UIViewController's view inside the "Content"'s view. This way, you can achieve a view that holds common data, while the only thing you do is to change the view inside the "Content"'s view.

Related

Is there a way to create a header for multiple View Controllers in xcode?

I need some help on creating a "Header" for multiple View Controllers without copy/pasting every time the line of code into the new view controller I create.
Something like creating a header in PHP and including it into the pages you want.
In the header I want to add custom text/data.
I tried creating a View on a single View Controller and then implementing that view on multiple storyboards, but it doesn't work and doubt that's the way of doing it.
I tried looking for something similar to what I needed but couldn't find it.
I am new to swift/xcode.
Thank you
Create subclass of UIView with nib.
When adding instance of this view to view controller, also create
constraints(or create them with interface builder).
Add setup
method that takes String argument and sets the label outlet.
If you meant header that will push all the view controller content down, that's hard to implement and I don't advice it.
I need some help on creating a "Header" for multiple View Controllers without copy/pasting every time the line of code into the new view controller I create.
There are at least three ways to do this:
Common view: Create a common view that you just add to each scene where you want the header displayed. RealNmae gives pretty good instructions for that, as does the possible duplicate that matt linked in a comment, so I won't try to describe that approach again.
Inheritance: Put the code to create the header in a view controller class that's otherwise empty. You might call it HeaderViewController or something like that. Then make all the view controller classes that need to display the header subclasses of that HeaderViewController class.
Containment: Create a container view controller that displays the header. Container view controllers can draw part of your UI, and then let a contained "child" view controller handle the rest. UINavigationController and UITabBarController are examples of container view controllers -- they draw a bar at the top or bottom of the screen that provides some functionality, and everything else gets drawn by the contained view controller(s).

When to use xib and when to use ContainerView?

I want to know when to use xib and when to use UIContainerView?
I didn't find any article related to this topic in the web. What are the differences and what are the usage of each of them?
Let's first talk definitions:
A XIB file is a graphical representation of a screen/view.
A UIContainerView is exactly what it says, it's a view that will contain child view controllers.
So based on that it is clear that they will not be used in the same way.
A XIB file is a base file you create in Xcode, link to a UIViewController and in which you can drag and drop elements to design your screens.
UIViewContainer is one of those draggable elements and is simply meant to be a reserved space in your xib (or storyboard if you use that instead) in which you can easily "embed" another UIViewController.
Do you sometimes add a child UIViewController to your main UIViewController? You'd do something like this in code:
Instantiate second controller
Add second controller's view as subview in main controller
Add second controller as child of main controller
Create constraints so that view shows exactly where intended
Well the UIContainerView is here to do exactly this, only now you don't have to do it in code, you can drag a UIContainerView in you XIB/Storyboard and link the second controller there directly!
You can use .xib design file for a standalone View (like custom datePickerView, CustomAlertView, customViews etc). Use can use this class where every you want.
But ContainerView, you can design with a UIViewController on storyboard and its automatically generate its own view for you.
This is embedded with a single ViewController.

Storyboard done, Do I need to create .h and .m View Controller file for each View created?

I have created Storyboard with several views calling each other, now I need to create the code
I notice that XCode didn't created .h and .m controller files for each View from storyboard.
Should I create them manually?
Should I keep only one controller? (or few depending of separation of concerns on MVC)
Is there a pattern for developing this?
thanks
The usual approach is one view controller pr. screen full of content. You can imagine having one view controller for a tableview, with any sort of content, and then another view controller that presents that content in a new screen full of content if a row is pressed.
Normally when you have subviews inside of your view controllers, you wire them up in interfacebuilder. Then for instance if you want to populate a view that has a uiimageview and a uiactivityindicatorview inside it, you can control their behavior and how their populated from the view controllers code. You could also if you want something very generic and you feel that one view will probably take up a lot of code in your view controller, create a uiview subclass for it, and then set the class in interface builder.
Did this help? Please let me know if you need more clarification.
It's entirely up to you whether you have a ViewController for each view. If you have many views I would recommend it. Even if you have 2 or 3 views you probably still should. Things can get really confusing when each view has a different task but all have similar IBOutlets.
TLDR; Personally, I would say it was good practice to have a ViewController for each view if each view has a separate task.

Using 2 views in 1 xib file

I want to create a view that will appear only once (on first time). After, I want to show another view.
Can I declare 2 views in 1 xib file?
Yes but you you will need some funky looping code to extract the proper view or you could get the view via index, if you know it will always be at a given index. Personally this is not an ideal solution for me because I like to have strong pointers to all objects.
Your Could create a ViewController subclass with 2 views and then bind each view to variable via interface builder. This might be a little cleaner?
You can, but there's no reason to do so. Just use two separate view controllers, each with their own view (in separate nib files).

iOS: When to use multiple NIB's or multiple views on same NIB?

Can anyone explain when it's better to use multiple views on the same NIB file than using multiple NIB files?
A NIB file can contain more than one view, so there should be times when it's better to use one method than the other.
Have i been too vague?
Best Regards
Fak
Personally I think using one view per NIB is better because you can use the name of the NIB file to load it. It also makes it easier to deal with changes while your App is evolving and helps if multiple developer / creative designers are working on your App.
The only place where I could thing of a good reason to put multiple views into a single NIB is when the views belong together. An example would be multiple cells for a single table which are then selected by the data type of the row or the section they are used in.
The downside of having multiple views in one NIB is that you need to load the views manually when creating the View Controllers so I suggest only using a single View per NIB for view controllers.