开发者

Delegate textShouldBeginEditing is not called in subclass of NSTextView

开发者 https://www.devze.com 2023-04-10 11:21 出处:网络
I\'m writing an application on Mac OS X (Lion) which is a job management program. The data source is MySQL.

I'm writing an application on Mac OS X (Lion) which is a job management program. The data source is MySQL.

In one window I have several NSTextFields which are subclassed to my own subclass of NSTextField. My subclass has the name HDLTextField. Inside my subclass I have implemented the delegate textShouldBeginEditing which is working great.

Now I have a NSTextView in my window which is subclassed to my subclass HDLTextView with its superclass NSTextView. But the implemented delegate textShouldBeginEditing is not called, allthough NSTextView as a subclass to NSText supports this delegate.

So why does the delegate work with NSTextField but not with NSTextView?

@implementation HDLTextView

- (BOOL)textShouldBeginEditing:(NSText *)textObject
{
    DLog(@"huu");
    if ([[[self window] windowController] recordShouldEdit]) {
    [[[self window] windowController] sendDocumentEdited];
    return YES;
}
    return NO;
}

- (void) setItDisabled: (NSNotification *) notification
{
    [self setEditable:NO];
    [self setBackgroundColor:[NSColor controlColor]];
}

- (void) setItEnabled: (NSNotification *) notification
{
    [self setEditable:YES];
    [self setBackgroundColor:[NSColor controlBackgroundColor]];
}

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void) awakeFromNib
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(setItDisabled:) 
                                             name:@"HDLRecordLockedByOtherUser"
                                           object:[[self window] windowController]];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(setItEnabled:) 
                                             name:@"HDLRecordReleased"
                                           object:[[self window] windowController]];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"HDLRecordLockedByOtherUser" object:[[self window] windowController]];
    [[NSNotificationCenter defaultCenter] removeObserver:self 开发者_运维问答name:@"HDLRecordReleased" object:[[self window] windowController]];
    [super dealloc];
}

Any help is very much appreciated.

BTW - while I'm writing this application as a newbie, stackoverflow gave me all the answers I was looking for. So, thank you very much everybody.


Apple provides this overview of delegation.

According to the NSTextField documentation, its textShouldBeginEditing: method requests permission to begin editing a text object. This is not a delegate method – it's a method of NSTextField which subclasses can implement.

To use delegation, you would instead create a different object which implements the NSTextFieldDelegate protocol, implement the control:textShouldBeginEditing: method, and set it as the text field's delegate. You can do this by connecting an outlet or by calling setDelegate on the text field.

NSTextView does not have a textShouldBeginEditing: method.

So, try connecting your NSTextView's delegate outlet to itself.

Or, use the delegation pattern as it's intended:

  1. Rename your class to HDLTextViewController, subclass NSObject, and implement the NSTextViewDelegate protocol.

    @interface HDLTextViewController : NSObject <NSTextViewDelegate>   
    
  2. In that class, implement the parent protocol's textShouldBeginEditing: method.
  3. In the nib, insert an NSObject and set the custom class to HDLTextViewController.
  4. Replace your HDLTextView with a standard NSTextView and connect its delegate outlet to your new controller object.
0

精彩评论

暂无评论...
验证码 换一张
取 消