Adding custom objects to object library - xcode

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?)

Related

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.

How to create custom Object Library items?

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.

How do I programmatically change a custom view object's member variables?

I appreciate you taking the time to read this and hopefully help me out!
I am writing my first program in Xcode 4, using Objective-C and Cocoa for the first time. I have been following the tutorials, but at this point I want to display custom graphics using a custom view, where the graphics change based on the values in my model object.
My controller class, GameController, based on NSObject, creates my model object during initialization, and successfully receives button action messages, updates the model object appropriately, and updates text strings on the window in return using its outlets. I accomplished this by creating an NSObject object in my xib file (which is called the default (?) name of MainMenu.xib, but it includes all the UI in my application), using the utilities pane to set its class to GameController, and then connecting its actions and outlets to the appropriate UI elements.
I have dragged a custom view into my window, and used the utilities pane to set its class to my custom view class, BoardView, which is based on NSView. In BoardView, I have overridden drawRect: to fill in the background of my view with a blue color, and then draw each of the graphics defined by my model class (GameStatus).
The problem is, I do not know how to access the data in GameStatus from my BoardView class, or have GameController update a member variable of BoardView. I have not instantiated BoardView anywhere besides in Interface Builder, by dropping a custom view on my window. I do not have a BoardView object in my xib file (the column or list on the left side in Interface Builder).
I tried to create an outlet to a BoardView object in my GameController class (which does have an object in my xib, as I mentioned above), and connecting that outlet to the BoardView custom view in my window, and Interface Builder seemed fine with that. However, when I run the program, the value of the BoardView outlet pointer in my GameController class is 0x0. It seems like the outlet is not being connected to a BoardView object. I don't know if I need to do something else in Interface Builder to make an actual object (I tried creating one in the list to the left, but then couldn't figure out a way to connect it to the actual custom view displayed on the window).
To add to the confusion, when I run my application, the BoardView area of the window will display the blue background, and in fact any other graphics which I define in the drawRect: function. However, without any way to talk to my model object, I can't change the graphics based on the state of the model. I'm not sure if the fact that the hard-coded graphics are displaying correctly means that there is an object there somewhere, or whether it is somehow drawing based on the general template of the class somehow. The latter doesn't really make sense to me, but if the former is true, I'm can't figure out how to talk to that object from other parts of my code.
I feel like I'm just missing some basic understanding of how Xcode / Interface Builder creates objects or something. I would really appreciate someone telling me exactly what I'm missing here in the connection between my MVC objects / classes.
Thank you in advance for any help you can give me!
EDIT 2011/09/06:
To download a copy of my project to take a look at it, go here:
http://talix.homeip.net/2011/rival/
That's my home server and I'm an amateur at this, so please leave a comment if it isn't working. Thanks! Also, if there is a better way to respond to comments other than editing my original post, please let me know; I'm also new to this website. ;-)
-Joe
It sounds like you're only instantiating one GameController and one BoardView, and that's happening in the nib.
This, then, is the heart of the problem:
I tried to create an outlet to a BoardView object in my GameController class (which does have an object in my xib, as I mentioned above), and connecting that outlet to the BoardView custom view in my window, and Interface Builder seemed fine with that. However, when I run the program, the value of the BoardView outlet pointer in my GameController class is 0x0. It seems like the outlet is not being connected to a BoardView object.
Where in your code are you needing a reference to your BoardView but getting nil? Is that in GameController's init method, or in some other method of GameController?
If it's inside init, this is what I'd expect. The code that loads nibs must initialize the objects before it connects the outlets.
(If it's outside of init, I'd suggest starting by disconnecting and reconnecting the outlet.)
View = BoardView
Controller = GameController
Model = GameStatus
In MVC, the controller usually brokers communication between the model and the view, so I suggest you handle it this way:
Add a gameController outlet to BoardView and connect it to your game controller.
In drawRect, have the BoardView get the game status from the game controller.
I also suggest you make GameController your application's delegate:
Delete RivalAppDelegate.[hm].
In your nib, delete the reference to Rival App Delegate, and connect the Game Controller reference to the File's Owner delegate outlet.
You've got two instance of BoardView in your nib. Hook up the one inside your window to the boardView outlet of GameController and delete the other.
In GameController.h, after #interface GameController : NSObject, add <NSApplicationDelegate>
In GameController.m, implement applicationDidFinishLoading and set up your application there. (You can even call setGameStatus there if you want to.)

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.

How to get notifications of NSView isHidden changes?

I am building a Cocoa desktop application. I want to know when a NSView's isHidden status has changed. So far using target/action doesn't help, and I can't find anything in NSNotification for this task. I would like to avoid overriding the setHidden method, because then I'll have to override all the NSView derived class that I am using.
UPDATE: I ended up using KVO. The path for "isHidden" is "hidden", probably because the setter is "setHidden".
You could use Key-Value Observing to observe the isHidden property of the NSView(s). When you receive a change notification from one of these views, you can check if it or one of its superviews is hidden with -isHiddenOrHasHiddenAncestor.
A word of warning: getting Key-Value Observing right is slightly tricky. I would highly recommend reading this post by Michael Ash, or using the -[NSObject gtm_addObserver:forKeyPath:selector:userInfo:options] method from the NSObject+KeyValueObserving category from the Google Toolbox for Mac.
More generally, one can override viewWillMoveToWindow: or the other related methods in NSView to tell when a view will actually be showing (i.e. it's window is in the window display list AND the view is not hidden). Thus the dependency on KVO for the 'hidden' key used above is removed, which only works if setIsHidden has been called on that view. In the override, 'window' (or [self window]) will indicate whether the view is being put into a visible view hierarchy (window is non-nil) or being taken out of it (window is nil).
I use it for example to start/stop a timer to update a control from online data periodically - when I only want to update while the control is visible.
Could you override the setter method for the hidden property so that it will trigger some custom notification within your application?

Resources