Cooking Cocoa - First steps in Cocoa
A noobs blog on learning Cocoa from scratch
Sunday, November 07, 2010
Out-Of-Scope, or is it?
Subversion 1,2,3
Sunday, March 22, 2009
A new try
Distributed revision control
- creating another Subversion repository on my laptop and manually copying back and forth
- changing the Subversion access from file to ssh and configuring my router to be available over the internet
- changing the Subversion access from file to ssh and not having versioning when not logged in my network
- changing to a centrally available repository (not in my network)
Sunday, February 01, 2009
nib vs xib
- xib --> xml
- nib --> binary
Sunday, January 11, 2009
IBOutlet / IBAction Definition
One of the most important concepts of Cocoa (at least to get us started) is the way that Cocoa combines UI and code.
In a Visual Studio project the code that generates the UI is in the same file as the coding itself.
To get it connected we need to let the code know what interfaces we have and let the UI know what we are working with. We will do this by defining Outlets and Actions in our code, creating an “interface object” in the UI that will be the interface between code and UI and connect the UI elements to that interface.
So what are Outlets and Actions?
- linking the button to an action of an “interface object”
- calling the method of that action in the code
So let’s get started:
First of all we will define the outlets. We will only need to call the window to close it, so we only need an outlet IBOutlet NSWindow *myWindow; The strange thing for me was that we have two definitions for myWindow (IBOutlet and NSWindow)..... but I guess you get used to anything, and that is just the way it goes.
Further we need an action for the time the button gets pressed on the UI.
-(IBAction)closeWindow:(id)sender;
Once we saved the file we can switch to the UI in the Interface Builder. Here we add a NSObject whichs Identity we will set to the class in which we added our outlets and actions. Then we create the UI by opening the Window object and dragging a button from the library (Command Shift L) to our window. Now we will set the link from the button to the action. To get this done we need to right click on the button and drag it on the newly created NSObject item. A new popup will appear listing all available actions. You should select the one that you have created.
Switch to Xcode and go to the source file to create the method that you want to add. Between the @implementation and the @end enter the method:
-(IBAction)closeWindow:(id)sender{[myWindow orderOut:sender];}
This way you can also get/set the text of text fields ([textField stringValue]), enable/disable buttons([myButton setEnabled:YES/NO]) or whatever you want.
Saturday, November 22, 2008
Objective-C
@interface AppController : NSObject {
int x;
}
@end
Now this class definition would not do to much as it does not have any methods, but it would define a class named "AppController" being derived from NSObject with one integer variable called x.
The definition always starts with "@interface" and ends with "@end"
A method definition in Objective-C will be defined like this:
- (int) setX:(int)y;
This defines a method setX with a return value of type int. To call the method you'll need to pass a variable of type int which can be accessed by the name y. The "-" in front of the method declaration is not a typo, but tells you that this is a "regular" instance method. A global class method would be defined with a "+" instead (comparable with the "static" keyword of Java/C#). The method definition will not be inserted into the curly brackets but between their end and the "@end" so the new class definition would look like this:
@interface AppController : NSObject {
int x;
}
- (int) setX:(int)y;
@end
The source file
To finish off our example here we'll have a quick look at the corresponding source file:
#import "AppController.h"
@implementation AppController
-(int) setX:(int)y
{
//Magic happens here.....
x=y;
return x;
}
@end
This should not be to much of a suprise now. Pretty much straight forward besides the "@implementation" and "@end" commands which simply wrap up the class related coding.