How to create custom Object Library items? - cocoa

I have written a custom view class that has two sub views, very similar to NSSplitView. I would like my new class to show up with two sub views when it is used in interface builder. The two subviews can be any NSView, but need to be specified by the developer.
How can I add my class to the Object Library?
How do I specify to IB that my class that it has two required subviews (IBoutlets)?
Thanks in advance,
Joe

Beyond making the subview properties IBOutlets, it's not possible to customize the way Interface Builder presents custom classes in Xcode 4. It used to be possible in (standalone) Interface Builder before Xcode 4, but that functionality was removed.

Related

Xcode Interface Builder - reusing a UIView

Is it not possible to reuse a UIView with constraints etc, from one view to another. I have tried:
Note: I am not in storyboard, but using xib file in a framework that I am creating - therefore I am using only UIView from xib files.
Tried to reference view in the same xib file.
#IBDesignable incl all custom classe setup.
I am only looking for reusing a UIView incl. constraints.
Regards
You can't do this through the storyboard because constraints are not made to be generalized through viewcontrollers. If you copy and paste constraints, they'll look for the exact views they used to constrain which won't be there anymore.
However if you have multiple views that look similar, then my suggestion would be to make a base class where you layout the view once on the storyboard and then subclass it. Or, your other option would be programmatic.

Xcode, Custom Class meaning

I searched all around stackoverflow and other websites but can not find a very simple thing. I suppose, it could have been answered already, then, could anyone just link the answer?
I am interested in what "custom class" in Xcode is in attributes inspector and how it works, what it actually does. I know very well how to make things work, but I do not have understanding of what I am doing.
To clarify. We can create a UITableViewCell class with, say, name cellClass, then we create .xib with the same name. Inside of .xib we drag UITableViewCell on the canvas and select its custom class - the name of the UITableViewCell class created before - in attributes inspector. So, what is this custom class in the attributes inspector is not clear for me. The example is very specific though, since we are using custom class for other things.
Thanks.
I think you are talking about the possibility to change the class of a predefined object in a .xib. If you insert a button to the interface, its class is NSButton, this to say that you instanciate an NSButton. But, you may have the need to extends NSButton into a MyButton class with specific added properties, methods, etc. If you want to instantiate it through .xib then you just have to drag'n'drop an NSButton and change its class to MyButton in the Identity inspector tab, Custom class form.
It is not very frequent to use NSButton this way, but much more common for some other objects, NSView for example (read Apple docs on subclassing NSView)
Are you talking about (Extensions, categories).
Please refer to this link:
Extensions
If you having issues in creating new subclases in xcode 7.1 then please click on:
File->New->Cocoa Touch Class-> Classname and select subclass of, it will works and you will be able to assign classes to uiviewcontrollers as well.

Adding custom objects to object library

How do I add custom objects to the custom object library in xcode?
I created a class myObject and I want this new object to appear in my object library list for use with IB.
This is not a direct answer, but if your custom object is a subclass of something else, e.g. NSObject or UIView, etc, then you can simply select the parent object and then change the identity to your subclass with the attributes window.
If you want a direct answer, then read this post: How do you display custom UIViews in InterfaceBuilder? for instructions to create a plug-in for Interface Builder that uses your custom class.
Xcode 4 doesn't support IB plug-ins anymore. You're out of luck.
(This is a direct quote from a comment of one of the answers to How do you display custom UIViews in InterfaceBuilder?)

How to create a reusable form using Cocoa bindings?

I want to make a user interface in which the user can edit two objects at the same time. The main window would have a vertical split view and a form on each side of the view.
The problem is that the two forms are identical and I don't want to duplicate the view components in the interface builder. I want to create the form one time and add a reference to it in each side of the split view, each one using a different object source.
I could use a NSForm, but the form is not a simple grid of outputTexts and inputText. They have a master table, and diverse kinds of inputs types, like combos, in the detail.
How do I create the reusable form using the interface builder? Or how can I do it programmatically? Do I have to create a subclass of NSView and add the individual components in the code?
Thanks,
Juliano
Similar to the way you create a NSWindowController subclass which manages a nib containing your window, you can create a NSViewController subclass which manages a nib containing the view, then instantiate it twice and (programmatically) add the two views to each of the subviews of the split view. See the code in this question for a (partial) example.
If you build one form in IB, you can option-drag it to the other side of the split view to create an exact copy, including bindings and connections.

Why do UIViewControllers have xib files and UIViews do not?

When I create a new UIViewController in xcode, it offers to make me an associated nib for the interface. However, when I create a UIView, it does not. If my understanding of MVC is correct, views should really be the parts that contain the interface elements (i.e. the nib) while view controllers are the parts that hook the functionality to the views they control.
I'm sure I'll be able to get it working together either way, so this is more of an exploratory question.
This seems like a case where I'm missing some fundamental understanding of how these two parts should be used. What am I missing?
Because UIView is usually not used in such way.
However How do I associate a nib (.xib) file with a UIView?
The answer I eventually got that satisfied my interest was roughly this:
The job of a view controller is to manage a view hierarchy. Interface Builder is an excellent tool for creating view hierarchies. Xcode offers to create a .xib when you create a new view controller because chances are high that you'll want to load the controllers' views from a .xib.
.xib files aren't necessarily meant to hold every UIView (or subclass) that goes into the view, just a general outline of views that don't change during the life of the view. The other UIViews are much easier to create programmatically since they change often.
I had a similar confusion. Basically (according to the MVC) the View is contained inside the Controller. In the iPhone this means that UIViewController has a property called 'view' which is a UIView instance.
A XIB file (and this is not mentioned often) is a serialised UIView instance. It is roughly an XML sub format which represents a UIView with all its subsequent views. So when you create a UIViewController, a UIView is created in the form of a XIB and bounded to that controller.
A new UIView therefore does not have a XIB because they are essentially the same thing...

Resources