With regard to my problems with delegates, I think maybe my frustrations stem 
from something about the delegation process or about text fields that I do not 
understand.

Here is how to recreate an example of my problem.

STEP 1: Start a new project called 'WhatKb'

STEP 2: Add a Cocoa Objective C class called MyController and fill in the .h 
and .m files thusly:

//  MyController.h

#import <Cocoa/Cocoa.h>

@interface MyController : NSObject {
 IBOutlet NSTextField* tf1;
 IBOutlet NSTextField* tf2;
}
-(void)awakeFromNib;
-(BOOL)textShouldBeginEditing:(NSText*)textObject;
-(void)textDidBeginEditing:(NSNotification*)aNotification;
+(void)logCurrentInputSource;

@end

//  MyController.m

#import "MyController.h"
#import <Carbon/Carbon.h>

@implementation MyController

-(void)awakeFromNib {

 // Prove that tf1 and tf2 are 'live' by programmatically giving them default 
values
 [ tf1 setObjectValue: @"TextField1" ];
 [ tf2 setObjectValue: @"TextField2" ];

 // Try to ensure that MyController is the text fields' delegate
 [ tf1 setDelegate: self ];
 [ tf2 setDelegate: self ];
}

-(BOOL)textShouldBeginEditing:(NSText*)textObject {
 [ MyController logCurrentInputSource ];
 return true;
}

- (void)textDidBeginEditing:(NSNotification*)aNotification {
 [ MyController logCurrentInputSource ];
}

+(void)logCurrentInputSource {
 TISInputSourceRef cis = TISCopyCurrentKeyboardInputSource();
 NSString* name = (NSString*) TISGetInputSourceProperty( cis, 
kTISPropertyLocalizedName );
 NSLog( [ NSString stringWithFormat: @"Current input source '%s'", [ name 
UTF8String ] ] );  
}

@end

STEP 3: Save the files.

STEP 4: Double-click on MainMenu.nib and add two text fields to the main window.

STEP 5: Instantiate an NSObject, and then in the Identity Inspector, pull down 
the Class combo box and select MyController.

STEP 6: In IB's Doc Window, Control-Click on MyController to bring up a window 
where you can connect tf1 to the first text field in the main window, and tf2 
to the second. These connections are very important.

STEP 7: Control-click on the first text field in the main window and connect 
its delegate to MyController. Do the same for the second text field. These 
connections should not be important because we try to do them in awakeFromNib 
anyway.

Save everything, build and run. The window will show up with the text fields in 
place and with their text already set by awakeFromNib. However, you can edit 
them all you like and see in the log that textShouldBeginEditing and 
textDidBeginEditing are never called.

I copied those method signatures directly from the NSTextField reference in the 
documentation, to eliminate any possibility that a malformed method signature 
could prevent delegation from working as expected.

I am sure the reason why these methods never get called is painfully obvious to 
one of you more experienced Cocoa programmers, but there is some piece of 
information that I don't have which is preventing me from writing or hooking up 
these methods properly. Can you tell me what it is?

Also, there is a reason I am using this WhatKb app to ask my delegation 
question. When we get the delegation to work and the methods are called, I 
believe we will find that TISCopyCurrentKeyboardInputSource() can frequently 
return the wrong information, and so I will have followup questions! :-)

Thanks! 
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to