How can I make a custom class be displayed in MainWindow.xib? - iphone

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.

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.

Classes for new View Controller in Storyboard

In my storyboard I drag on a new View Controller. My Storyboard now has two view controllers: the main one that came when I created the file, and the one a dragged on.
When I go into the 'assistant editor' and select the main view controller, I get the ViewController.h class. But when I select the other controller I get UIViewController.h which is an Apple file.
How do I link/create these classes for each View Controller? Is there an automated way to do this, or am I not doing it right.
You need to create your own subclass of UIViewController and set the newly created view controller as the custom class in the storyboard.
Press cmd+n or go to File > New File
Select Objective-C class and hit next
Type UIViewController into the second box and type a name for the new class in the first box (which will be something like MyClassViewController)
Go into your storyboard, select the View Controller you dragged out, look at the inspector and go to the Custom Class Tab and set the custom class to your newly created view controller (e.g. MyClassViewController)

How to make a derived UITableVIew work

I've made a new class in my Xcode project which is derived from UITableView
I then drag a UITableView from in the interface builder onto my view and change the class of the object to my derived class. Then I drag the outlet from the table to the files owner and hook it up to an outlet variable in my main view, which is of the same type as my derived class.
How do I also set up the datasource and delegate so that they belong to the derived class?
I've got two table views on my main viewcontroller, and one of them uses that controller itself which works fine. The delegate and datasource is the file owner of the view controller.
But my other table view will be for different data, so I want it to use the datasource and delegate which is implemented on my derived tableview.
When you click on the UITableView-derived object are you not able to see the delegate and datasource members under the Table View Connections pane of the Inspector window? It should be as simple as dragging a connection from each to file's owner, or whatever object you want to make delegate/datasource. I've seen them go away when all you do is change the class type of an object, but if you write headers and make sure your class says that it is derived from UITableView then return to IB I believe you will see them again.
(apparently IB2 used to handle this the way you expected - in IB3 it is best to derive in Xcode first, then use the derived class in IB)

UIViewController with custom UIView

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.

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.