0
votes

I have a NSTextField object in my window which has to be disabled when a check box is clicked.

I have written a IBAction to receive the check box click and disabled/enabled the text filed based on the check box state.

[mName setEnabled: [mNameCheck state]];

This work fine with the basic functionality, but I found some strange behavior.

You update some detail in the text filed and click on check box the text filed get disabled old content.

Example:

  • Stage 1: Text filed has the content "Name"
  • Stage 2: Update the text filed content as "Girish"
  • Stage 3: Click check box (to disable the text filed)
  • Stage 4: Text filed disable with the content as "Name"

The issue get resolved if I resign the responder and set responder to some other controller before the text field is disabled.

In my case I can not assign the responder to check box(it does not take) or any other controller so I did some thing like bellow which works fine

[mName resignFirstResponder];
[mName becomeFirstResponder];

resign and assign responder with same controller.

I am just wondering is this solution is correct or any better solution to this issue?

2

2 Answers

3
votes

As the docs state, do NOT call -resignFirstResponder or -becomeFirstResponder directly. Call -[NSWindow makeFirstResponder:] instead. It is acceptable to pass in nil and status will pass to the window itself.

You could try calling -[NSWindow selectNextKeyView:] although I'm not entirely certain what will happen if it doesn't find a valid next key view. Try it and see. If that doesn't work you'll have to fallback to calling -nextValidKeyView and -makeFirstResponder yourself.

0
votes

If you set the text field to be Continuous in Interface Builder, the value of the field will be set as soon as a change is made in the field rather than when it loses focus. You can programmatically set this value with [yourTextField setContinuous:YES].