从其委托通知中访问NSTextField ...

时间:2012-01-16 09:47:27

标签: delegates nstextfield nsnotifications

我正在继承NSTextField

MultiTextField.h

#import <AppKit/AppKit.h>

@interface MultiTextField : NSTextField {
    id storedObject;
}
@property (nonatomic, retain) id storedObject;
@end

MultiTextField.m

#import "MultiTextField.h"

@implementation MultiTextField
@synthesize storedObject;
@end

存储指向对象的指针,我想“重命名”。

我使这个文本字段可编辑,并有一个代表听取controlTextDidChange:并且工作正常:

- (void)controlTextDidChange:(NSNotification *)aNotification {
    NSTextView *textView = [[aNotification userInfo] objectForKey:@"NSFieldEditor"];
    NSString *theString = [[textView textStorage] string];

    if([theString length] > 0 ) {
        MyObject *theObject = ???; // I need access to the MultiTextField.storedObject!
        [theObject setName:theString];
    }
}

唯一的问题是我无法访问storedObject(请参阅if-block中的注释)。

那我该如何访问那个storedObject?

1 个答案:

答案 0 :(得分:1)

试试这个:

MyObject *theObject = [[aNotification object] storedObject];