Strictly Programming

A Simple Cocoa Binding in XCode 4.6

Posted in KVC, Objective-C, OS X by sqllyw on 08/27/2013

This is just a very simple sample about Cocoa Binding in XCode 4.6..

XCode has changed a lot since the last time I used, which was XCode 3.2, It has made the KVC much easier. Following is a simple project for the xCode 4.6:

1. First, create a new project, name it ‘binding1’

pic1

2. Add an Objective Class: Client, you got two files, Client.h and Client.m:
Client.h

#import 

@interface Client : NSObject

@property (copy) NSString *name;
@property (copy) NSString *email;

@end

As of now, we should have following files in the project:

pic2

3. let’s create an instance of Client in code, first modify AppDelegate.h to be like following:

#import <Cocoa/Cocoa.h>
#import "Client.h"
@interface AppDelegate : NSObject <NSApplicationDelegate> {
  Client *client;
}
@property (assign) IBOutlet NSWindow *window;
@end

and AppDelegate.m to be:

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
}
- (id) init {
    self = [super init];
    if (self) {
        client = [[Client alloc] init];
        [client setValue:@"new client" forKey:@"name"];
    }
    return self;
}
@end

4. As of now we have a project with a model class, Client, and an instance of Client instantiated when the program starts, let’s proceed to bind some controls to the program:

click MainMenu.xlib under ‘Objects and Controllers’, drag a ‘Object Controller’ into the Dock, select the object controller just created, click its ‘Attribute Inspector’, do the changes as below:

pic3

This is to inform the Object Controller what class it will handle, it’s not enough, we need to tell the controller where to locate the instance of the class created in #3, click ‘Binding Inspector’ and do the following bindings:
pic4

5. Now, click the window, drag one NSTextField and one NSButton into it, select the NSTextField, do the following binding:
pic5

6. Create an action in AppDelegate.m, and set the NSButton target to it:

- (IBAction)doSubmit:(id)sender {
    NSLog(@"name is %@",[client valueForKey:@"name"]);
}

7. Run the program, make some changes in the edit box then click the NSButton, you should see the new value in XCode’s console.

8. Summary

8.1 create a model class
8.2 provide a storage for that
8.3 create a Object Controller to handle object in 8.2, set attribute, bind storage are the steps required.
8.4 use a NSTextfield, bind it to the controller in 8.2 and bind the attribute (model key path)
8.5 you can access the new value by using

[client valueForKey:@"name"]
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: