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.
Related
I have created a custom UIView class named "abc" and a nib with it. Created a UIButton in IB in that nib. Now when I try to create outlet of that UIButton by extending arrow to File's Owner it wont allow me to do so. Please tell What i am doing wrong
You need to change the view of your nib file to the customView class name, in the Identity Inspector, change the UIView Class in the picture to your custom class
Problem might be you are not setting Custom Class name for "abc" in xib file. Please not that whenever you create a subclass of UIView. You have to bind its view with that class. And when you are going to make IBOutlet connection instead of connecting it to File's Owner you have to connect it to you View placed in Objects(object list is on left side in interface builder).
*) Goto the views xib
*) Click on the File's Owner Identity inspector
*) and change the class name to the name of the View yu want.
I created a custom class that inherits from UIView and I want to display an instance on the screen. However, when I try to link the outlet from the App Delegate to the object I dragged onto the screen (which is that custom class I created) the link cannot be created.
What am I missing?
You should add a generic view in your window inside IB, and then, select this new view (let's call it myView) and go to the identity inspector (cmd+4) and write the class name of myView in the Class box inside the inspector. You may be able to link the iboutlet with your view now.
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.
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).
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.