0
votes

I know how I'd do this with NSTextView, but NSTextField doesn't seem to provide a way to access the NSTextStorage backing it, so I can't set myself as a delegate and process -textStorageDidProcessEditing:.

I have a NSTextField as part of a sheet. If that text field is emtpy at any point, the OK button should be disabled until some input is provided. That's basically all I'm looking to do, and I'm sure there's a really simple way to do this?

I tried:

[[[filenameInput cell] textStorage] setDelegate:self];

Thinking that NSTextFieldCell would provide the text storage (mostly since Xcode kindly auto-completed it for me) and then of course, did my validation via the delegate method:

-(void)textStorageDidProcessEditing:(NSNotification *)notification {
    BOOL allowSubmit = ([[filenameInput stringValue] length] > 0)
        && (([relativePathSwitch state] == NSOnState) || ([[localPathInput stringValue] length] > 0));
    [createButton setEnabled:allowSubmit];
}

This compiles but causes a runtime error as NSTextFieldCell does not respond to textStorage.

What's the standard pattern I should be following here? This must be one of those "every day" tasks for Cocoa devs, I imagine :)

The Sheet

2

2 Answers

2
votes

Always the way... I found it. It's not listed on the page for NSTextFieldDelegate, or NSControlTextEditingDelegate, but on the page for NSControl itself.

The delegate method is: -controlTextDidChange:.

2
votes

This is what NSFormatter is for; create a subclass of NSFormatter and set an instance of it as your NSTextField's formatter, and let it validate the text the user enters.