An adventure in XCode and Cocoa

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.
IB Bindings
Then I ended up with a working Cocoa mac app



Legg igjen et svar

Fyll inn i feltene under, eller klikk på et ikon for å logge inn:

WordPress.com-logo

Du kommenterer med bruk av din WordPress.com konto. Log Out / Endre )

Twitter picture

Du kommenterer med bruk av din Twitter konto. Log Out / Endre )

Facebookbilde

Du kommenterer med bruk av din Facebook konto. Log Out / Endre )

Kobler til %s

Følg med

Få nye innlegg levert til din innboks.

Bli med 140 andre følgere