An adventure in XCode and Cocoa
Innsendt: 10.08.11 Arkivert i: code, Uncategorized Leave a comment »Since I haven’t learned a new language in a while, I wanted to try something new. Next stop Cocoa Mac and XCode.
It took quite a while to figure out how the Interface Builder and XCode worked together, but I’ll try to give a quick explanation:
First I created a new Application, and XCode created a XIB-file for me. This was called MainMenu.xib and contained both a system menu and a view. The XIB could be opened in Interface Builder, and I could draw the UI.
I then created a Cocoa class that inherited from NSView. In this class I implemented my properties and methods, which is called Outlets and Actions. Using the correct types is important for the Interface Builder to work when connecting code and UI together.
HelloWorldView.h
#import <Cocoa/Cocoa.h>
@interface HelloWorldView : NSView {
IBOutlet NSTextField *words;
}
- (IBAction)sayHello:(id)sender;
@end
HelloWorldView.m
#import "HelloWorldView.h"
@implementation HelloWorldView
- (IBAction) sayHello:(id) sender{
NSLog(@"buttonClicked");
[words setStringValue:@"Hello, world"];
[words setNeedsDisplay:YES];
}
@end
Then I hooked this class up to the View using Interface Builder.

Finally I hooked the text fields to the properties and the Buttons to the methods.

Then I ended up with a working Cocoa mac app
