IOS: How to create a Master View - iphone

I started a project with storyboards and it has a lot of views, each one of them has the same header (with an image, company name and two buttons). I want to do this once in the main View, and make it reusable for the other views. It’s like a master view with a header and footer. How could i do this?
This is my first project starting from cero and I want to make it as organized as possible. What are the best practices using storyboards and MVC?
Thanks in advance.

There is no special way of doing this in storyboard.
But you can do it easily by making custom UIView.
Make a sub class of UIView and pust all common design in it.
Place a UIView on top of every UIViewController.UIView.
Now set this top UIView's class with your custom class in identity inspector.
All the best.

A primitive way to do it in storyboard is to define there your master view, select all of its elements, copy them and paste them into the other views.

You can subclass UIViewController to add a header to its view, and UITableViewController to set it as the tableview header.

Related

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.

Same UITableView in different viewcontrollers?

I have an application that has multiple viewcontrollers and one common menu table.
Menu table is same in all viewcontrollers. Currently, If i want to add some more menu in my menu table then I have to change in all the view controllers.
Can we have a common table view which can be called in different viewcontrollers?
Thanks
Make a subclass of UITableView and create your menu in that like :
#interface menuTable : UITableView
Then you can use the instance of that class in all viewControllers.
Yes, you can.
When you add UIView to some other UIView, former will be removed from it's Superview and added to latter as subview. Remember to be careful with view's frame. e.g. convert it using convertRect:toView: selector
As I understand ur question, just create a tableView in CommonView (a ref. name) and as per your need you can call that table view. By this way you have to not change many times in your code.

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.

Best Practices for refactoring controls and functionality out of a XIB

I have a regular custom UIViewController that loads a regular iPhone-screen-sized XIB. But I have a subview in the XIB that consists of a few components (UILabel, UITextField, UIButton) that I'd like to factor into a separate XIB, and move the logic controlling these components out of the UIViewController, possibly to a custom UIView object that would load the custom XIB.
I've searched Apple documentation without much luck, and searched for an answer here, but I can't seem to find a clean way of creating a custom UIView, instantiating it in a XIB, and dropping it into another XIB. Is there a clean way of moving this view into it's own XIB and the logic controlling it out of the custom UIViewController, so I can use it on multiple screens of my app?
(I'm not married to the idea of putting this functionality into a custom UIView, it just seemed like the obvious way of doing it to me. I'm open to any clean way of factoring this into it's own component.)
If you want to know about multiple views in xib u can go through this
Multiple Views within one XIB - iPhone SDK
This may help you.
If you want to create a view in xib without view controller go through this
Using a xib file without initializing a view controller
I think this should be useful

Adding a TableViewController to an existing table view

I've got a Table view and it's the child of the View ( see the IB hierarchy ) :
I want to start customizing the tableCells, so I assume I'll have to add a tableViewController . Where would I start in this instance ? the TableViewcontroller can't be a child of the MainViewController, can it ?
Cheers,
You would need to add a table view controller at the top level.
Then drag connections from the table to the controller of datasource and delegate
Then drag connections from the controller to the view for "view"
Then change the class of the controller you added (it's the last tab in the inspector) to your custom UITableViewController class (if you don't subclass it it does nothing)
Then make sure that you somehow retain the controller when the nib is loaded. Easiest way is with an IBOutlet in the file's owner.
Sorry if this is a bit general, but the specifics would consume pages. I don't recommend this approach unless you have some experience with this and know what most of the above means, in which case it's possible and sometimes desirable to do this, and good job if you do.
The easiest method, if you just want a simple table view, no special circumstances, is to use the File>New File>UITableViewController with xib for user interface, then go from there with the file it makes, but that isn't your question, so I have answered as best I can.
It would be easier to build a tableViewController class with a table inside that and add the tableViewController to the view instead of reverse-engineering it. The tableViewController will have all your delegate and datasource functions for your table fairly mapped out so you can move whatever you already have into those functions.
As for custom tableViewCells, here's a great tutorial from pragmaticstudio.com on how to do so. He walks you through all the aspects of building a tableViewController and also how to customize your cells in IB.