开发者

Validate a user interface (NSButton) as user type in NSTextField

开发者 https://www.devze.com 2023-02-01 22:46 出处:网络
I know how I\'d do t开发者_开发百科his 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 -textSto

I know how I'd do t开发者_开发百科his 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 :)

Validate a user interface (NSButton) as user type in NSTextField


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.


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:.

0

精彩评论

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