Pages

Sunday, April 7, 2013

Re: Beginning cocoa programming: delegates

I constructed another simple example of a delegate: I created an app with one lone NSTextField on the window.  I wanted to see if setDelegate: would work with a different widget(NSTextField v. NSWindow).  My goal was to see if I could setup a delegate using setDelegate: on an NSTextField, such that a delegate method would be called when I typed in the text field.  I  looked through the documentation, and I decided to to see if implementing this delegate method would work:

 

- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor;

 

 

I created a new app(deleting the classes that Xcode automatically creates, as well as the instance in the MainWindow.xib window in IB), and then I created this class:

 

 

//MyDelegate.h

 

import

 

@interface MyDelegate : NSObject {

}

- (BOOL)control:(NSControl *)control

          textShouldBeginEditing:(NSText *)fieldEditor;

 

@end

 

 

//MyDelegate.m

 

@implementation MyDelegate

 

- (BOOL)control:(NSControl *)control

               textShouldBeginEditing:(NSText *)fieldEditor

{

          NSLog(@"***In delegate method...");

          return YES;

}

 

@end

 

 

1) In IB, I created an instance of MyDelegate by dragging an Object from the Library onto the MainWindow.xib window.  I entered the name MyDelegate in the TextField Identity Inspector.

 

2) In IB, I ctrl-clicked on the NSTextField, and then I dragged a connection from NSTextField's 'delegate' outlet to the MyDelegate instance(in the MainWindow.xib window).

 

3) Saved everything, build&run, and when I typed in the text field, I saw the logged message in the Debugger Console, as expected.

 

Next, I attempted to create code that uses setDelegate: to achieve the same thing as above.

 

4) In IB, I ctrl-clicked on the NSTextField and deleted the connection from the 'delegate' outlet to the MyDelegate instance.

 

5)  I made these changes to the code:

 

//MyDelegate.h

 

#import

 

@interface MyDelegate : NSObject {

 

          IBOutlet NSTextField* textField;

}

- (id)init;

- (BOOL)control:(NSControl *)control

          textShouldBeginEditing:(NSText *)fieldEditor;

 

@end

 

 

//MyDelegate.m

#import "MyDelegate.h"

 

@implementation MyDelegate

 

- (id) init

{

          if (![super init]) {

                    return nil;

          }

 

          [textField setDelegate:self];

 

          return self;

}

 

- (BOOL)control:(NSControl *)control

          textShouldBeginEditing:(NSText *)fieldEditor

{

          NSLog(@"***In delegate method...");

          return YES;

}

 

@end

 

 

6) In IB, I ctrl-clicked on the MyDelegate instance in the MainWindow.xib window, and then I dragged a connection from the outlet 'textField' to the NSTextField(in the app's main window).

 

7) I saved in IB, then build&run (save everything), and this time when I typed in the textfield, the message was not logged to the Debug Console.

 

What is the matter with setDelegate:?  Why doesn't setDelegate: succeed in setting up the delegate so that I can see the message logged to the console?

 

Thanks


View the original article here

0 comments:

Post a Comment