Programming Mac OS X with Cocoa for Beginners 2nd Edition/Color Hello World
Creating a Custom View in XCode 4
[edit | edit source]Creating The Resources for a Custom View in XCode 4
[edit | edit source]1. Goto your project window for this project.
2. On the left side of your project window, click on your .xib file and your menus for this project will appear. This project's window may appear too. If it doesn't, click on the icon of the window. You may have to experiment with XCode to get the feel for it. It's hard to explain in this text.
3. Show the Objects Library if it's hidden. In the lower, right-hand portion of XCode's project window there should be a view that's labeled "Object Library". If not, from the "View" menu, choose "Utilities" and then choose "Show Object Library" and it will appear.
3. Find the view called "Custom View". There are a lot of classes of view in the object library. If you need to, you can search for "Custom" and that will quickly locate the class of view that you need.
4. Click and drag a Custom View object from the object library into your project's window.
5. Position and size your custom view and window.
Creating the Source Code Files for a Custom View in XCode 4
[edit | edit source]1. In XCode 4, from the "File" menu, choose "New". A hierarchical menu will popup, then choose "New File".
2. On the left-hand side of the window, you will two main categories of files to choose from: 1) iOS or 2) Mac OS X. Choose Mac OS X.
3. Another window will be displayed to let you choose the type of file to create. Choose "Objective-C Class" and click the "Next" button.
4. Yet another window will be displayed that will let you type in your class's name and choose what it is a subclass of. For the class name, type in "GCHelloView". For the "subclass of" field, use the popup selector and select that our new subclass is a subclass of NSView. Note that there are hundreds of classes available in Cocoa, and you could potentially subclass many, many classes, but commonly there are only a few that you may need to subclass so only those are available in the popup selector. Click the "Next" button.
5. Another window will be displayed that will allow you to save the Objective-C files that are about to be created. You choose both a "group" (which looks like a folder in your project window) and you choose an actual folder on your disk drive. Click the "Create" button.
6. By now, XCode should have created two files for you: 1) "GCHelloView.h" which is header file. It serves the same purpose as a C header file only it has additional things that Objective-C understands. and 2) a file called "GCHelloView.m" the dot-m is the extension for Objective-C implementation files.
7. Go ahead and open up the .h file It should look like the code below. It should 1) import the Cocoa Framework, 2) declare the class we just created, GCHelloView, to be a subclass of NSView which is a Cocoa class for things that are drawn.
8. Although the code below says "interface", this is a class declaration. (What's called an "interface" in Java is called a "protocol" in Objective-C.)
#import <Cocoa/Cocoa.h>
@interface GCHelloView : NSView
@end
9. Now, open the file, GCHelloView.m It should look like the following code.
Notice the #import directive. This is similar in purpose as #include in C, only in Objective-C, using #import is preferred since you don't have any problems if the same file is #imported more than once. Objective-C supports everything that C does so Objective-C does support the #include directive, but it isn't used often because #import works better.
Next, look at @implementation GCHelloView everything that has to do with this class's implementation goes between that and the @end
Notice, that XCode has created two methods for us.
1. initWithFrame: This is an "init" method. In Objective-C, objects are always created in a two-step process. First alloc is called and then an init method is called. XCode has setup this init method for us which will work OK without modification for this project.
2. drawRect: This method is whats used in a subclass of NSView to draw itself. This is where we'll put in the code from this wikibook.
#import "GCHelloView.h"
@implementation GCHelloView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
}
@end