Question

I can capture string when user click the button. and I also use the following method

- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor

which declared in NSControlTextEditingDelegate Protocol. And when user begin editing, the button will be available.

My Question is: How to make the button disable when user delete all text (make the textField empty without clicking button)?

The above method seems can not do it...

Was it helpful?

Solution 2

Look at using the NSControlTextDidChangeNotification or controlTextDidChange: delegate method (from NSControl) which is posted by the text field. When you receive the callback you can examine the text currently in the field to decide what to do.

OTHER TIPS

If your NSText is a NSTextView you may also use NSTextDelegate Protocol and NSTextViewDelegate Protocol.

Among others, the NSTextDelegate Protocol declares this delegate method:

textDidChange:

Informs the delegate that the text object has changed its characters or formatting attributes.

- (void)textDidChange:(NSNotification *)aNotification

Discussion

The name of aNotification is NSTextDidChangeNotification.

The NSTextDidChangeNotification is documented here NSTextDidChangeNotification

Many ways to do the same thing. This version lets you easily catch specific keystrokes of interest.

- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString{
     NSLog(@"last character entered: %@",replacementString);
}

for instance, if you were looking for the return key, you might then implement:

if ([replacementString characterAtIndex:0] == NSNewlineCharacter)
    NSLog(@"return pressed");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top