UIViewController with custom UIView - iphone

Is there a way to make e.g. UIViewController use a custom UIVIew class?

Yes, there is. Just create your custom class that inherits UIView, open interface builder, open the ViewController xib file, select the view, and change the Class on the View Identity tab.

You can create your custom view in the Interface Builder and reuse it in a Controller. I just found the answer at the bottom of this post:
http://fdiv.net/2011/10/29/reusable-views-ios
Download the example, there is no code in the files, only the connections used in IB and the skeletton of the custom view.

Related

How to create the UIView sub class with xib?

I want to create UIView commonly. And use that view in all the view controllers. How can create the UIView sub class with xib and use in all the view controllers?
Thanks in advance.
You need to create your subclass: File -> New -> File -> Cocoa Touch -> Objective C-class -> Subclass of Target Class Type (UIView on your case)
Then, you create the xib for it: File > New > User Interface > View (name your xib)
Now go and design your xib on the Interface Builder.
Last thing you should do is to change your view's Class Type in Identity Inspector. Your custom xib will be linked with your custom class.
Let me know if you need further detail.
A UIView subclass first of all must consist of some code defining it, regardless of how you're going to use this view. After you create this class, in order to use it you only need to define the class name in the Interface Builder. Select the UIView you added and change it's class name in the object inspector.
Keep in mind that new properties or functionality that you are adding to this UIView will not be accessible by editing the xib, but only by code.

How can I populate UITableView with XML?

I have a tab bar application. In one of my tabs, there is a search bar and a table view below that. When you enter something into the search bar, it returns parsed xml. I need to put this parsed information into the tableview below. The class inherits from UIViewController. I declared a UITableView object in the header file and linked it in interface builder, and adopted the UITableViewDelegate protocol.
I'm not sure If i'm going about this the correct way. Any help?
That sounds about right. You will need to also have your UIViewController implement UITableViewDataSource, and add the methods from that protocol to populate the table.
There are various tutorials available which guide you step-by-step to create a simple tablview. Google them. You can follow these steps for connecting your tableView outlets:
Select your viewController where you are displaying the tableview to be your "File's Owner" in Identity Inspector.
Drag your view's outlet to your File's owner.
For tableView inside view, drag its outlets to File's owner again so that your datasource and delegates are up. And in the same view, drag your referencing outlet to the IBOutlet you have created in your viewController class.

UIView or UIViewController for custom View

What is the best way to create a custom UIView that I can consume in Interface Builder?
Create a custom UIView in Interface Builder and inherit from UIView in a code file, then somehow use it in another UIView ala like a control. (How do I do this?)
Create a custom UIView in Interface Builder and have a custom UIViewController wire it up. In my main ViewController, place the new view.
Basically, I am trying to create a reusable display view and would like a quick way to change it across all my instances with minimal effort. I already have laid out my XIB in Interface Builder.
The best is the 1st way. And don't forget to place IBOutlet keyword before class member, that you want to see in Interface Builder.
#interface MyViewController : UIViewController {
IBOutlet UILabel *m_MyLabel;
}
....
You'll want to do (1). Presumably you've got an existing IB file in which you'd like to place the custom UIView subclass? In that case, go to that file, drag out a UIView, and then in the "Application Identity" tab of the inspector (4th tab) set the Class to your custom class (as defined in code).

Custom UIView built with Interface Builder accessible/positionable via Interface Builder

This shouldn't be this confusing. I have a custom UIView with a bunch on controls on it. UILabels, buttons, etc. I've created this Nib using Interface Builder. I want to be able to position this custom uiview on another UIView using the interface builder.
How do I link my UIView custom class, to the nib? initWithCoder gets called, but I want this class to get loaded from the nib.
Thanks
For this to work, you have to create a plug-in for Interface Builder that uses your custom control's class. As soon as you create and install your plug-in, you will be able to add by drag and drop, instances of your view onto another window or view in Interface Builder. To learn about creating IB Plugins, see the Interface Builder Plug-In Programming Guide and the chapter on creating your own IB Palette controls from Aaron Hillegass's book, Cocoa Programming for Mac OS X.
Here is the link to the original author of the accepted answer to a similar question.
You only need to make a plugin if you want the custom view to draw correctly in the nib you are using it. You can make a custom control and then have it show as a blank rectangle until instantiated during run right now.
How do I get a view in Interface Builder to load a custom view in another nib?
You can insert a UIViewController object using Interface Builder, and then set the UIViewController's "Nib name" property.
I don't know if this messes up your model, but I think it's the only way to do what you're trying to do.
In IB bring up the Identity Inspector tool (Command-4) then select your custom view and in the Class pop-up choose the name of your custom class instead of generic UIView. You may want to connect it to an ivar as well. In your ViewController declare an instance of your custom class with an IBOutlet in front of it. Then go back and bring up the Connections Inspector and connect your view to the ivar by click-dragging from the custom-view's referencing outlet to the File's Owner (which should be an instance of your ViewController) and choosing the ivar name.
When your NIB is loaded it should be creating an object of that type and connecting it to that variable.

How does Interface Builder know about UIViewController's view?

UIViewController has an ivar (and #property) called view. It is not however, an IBOutlet.
When creating a view nib in Interface Builder, you typically set File's Owner to be your UIViewController (or subclass thereof), and you wire the nib's view to File's Owner's view outlet.
How does this work if the UIViewController view member isn't an IBOutlet?
The IBOutlet keyword is used to indicate that a property should show up in Interface Builder for classes that you write. Interface Builder internally has additional knowledge about system provided classes.
If you are working with Interface Builder on the desktop, rather than iPhone, you can write Interface Builder Plug-Ins to integrate your custom classes into the Interface Builder Library (This is not supported in Interface Builder for the iPhone). In writing a plug-in, the plug-in author provides "class description" files that declare this same actions/outlets information to Interface Builder as one achieves with the IBAction/IBOutlet keywords. You can read more about it in the Interface Builder Plug-In Programming Guide.
It works essentially the same way for system provided classes as it does for plug-ins.
I read this article a while ago and it finally tied it all together for me. Maybe it'll help you?
The simple answer is that Apple wrote both Interface Builder and UIViewController.
The view property of UIViewController is just that, a Property. The whole purpose of a UIViewController is to control a view. The view property allows you to assign that view to an UIViewController. That's the purpose of the view and that's why it shows up in IB. It's a property. Just like textLabel is a property of UILabel.