Re: Cocoa n00b frustrations
Re: Cocoa n00b frustrations
- Subject: Re: Cocoa n00b frustrations
- From: "Vijay Malhan" <email@hidden>
- Date: Sun, 8 Jun 2008 12:04:36 +0530
On Sun, Jun 8, 2008 at 8:59 AM, Robert Nicholson <email@hidden>
wrote:
> You can try the following methods
>
> - (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText
> *)fieldEditor
> {
> [ MyController logCurrentInputSource ];
> return true;
> }
>
> - (void)controlTextDidBeginEditing:(NSNotification *)aNotification {
> [ MyController logCurrentInputSource ];
> }
>
> You seem to be confusing the methods you need to implement in your
> textfields delegate vs the methods of the textfield that calls the delegates
> methods.
>
>
> On Jun 7, 2008, at 10:44 PM, Charles Jenkins wrote:
>
> 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;
>>
>
Also,
The above 3 method declarations should not be here. As they are not part of
your formal class interface. That is, they cannot and should not be called
keeping your class or it's instance as a receiver.
These are delegate methods and need to be implemented in your implementation
file only. With those declarations in header file, the users of your class
will see them as instance methods of your class, which is not true and is
confusing.
Thanks,
- Vijay
>
>> +(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 (email@hidden)
>>
>> 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:
>>
>>
>> This email sent to email@hidden
>>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> 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:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden