Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField stringValue is not up to date when button is pressed

I have created a dialog window with an NSTextField and an NSSecureTextField (for a username and password), along with an NSButton to submit. The outlets are properly connected in Interface Builder (non-nil).

I am retrieving the stringValue of the username and password fields in the action handler for the button. However:

  • If I enter a character in the password field and press the button, its stringValue is empty.
  • If I enter a character in the password field ('a'), change focus to the username field, change focus back to the password field, and enter a second character ('b'), the passwords stringValue is 'a' rather than 'ab'.

The stringValue of the password field (as well as the username field) appear not to update their value while in focus. I have tried the following to correct this issue, to no avail:

  • Set the text field's Action to "Sent on End Editing"
  • Set the text field's State to Continuous
  • Called validateEditing on the text field in focus
  • Called resignFirstResponder on the text field in focus
  • Called [self.window makeFirstResponder:self.window]
  • Called endEditingFor: on the window with the text field as its argument (or nil)
like image 941
titaniumdecoy Avatar asked Mar 21 '23 20:03

titaniumdecoy


1 Answers

I found the solution to my problem. I was disabling the UITextField before asking for its value. For some reason, if I ask for the value before disabling it, the value is correct.

Update: The documentation for the UIControl setEnabled: method states that if its argument is NO, "any editing is aborted."

like image 74
titaniumdecoy Avatar answered Apr 07 '23 00:04

titaniumdecoy