Taming NSTextField with bindings and formatting for proper behavior

I want to have a window that should accept some user input, for this I have the following:

  • NSWindowdownloaded from nib using NSWindowController, which is also its delegate
  • Couple NSTextFieldwithNSNumberFormatter
  • Using bindings, NSTextFieldbound to integer properties in NSWindowController(I have not used NSObjectControllerfor simplicity, but I can add it if necessary)
  • A 'done' NSButtonthat the user clicks upon completion to accept the changes and close the window
  • In the controller, the windowShouldClose:method performs a final check and decides if I can close the window

What I want to achieve is pretty simple, but Cocoa insists on making it complicated:

  • NSTextField should take only a number for the final value, which is also greater than zero
  • If the user tries to insert non-numeric values ​​or zero, a warning should appear in which the user will have to correct his record
  • When the user clicks on:
    • Make another custom check (e.g. compare if one number is larger than the other)
    • If the values ​​are correct, accept the changes and close the window (the "model" should already be updated with the values ​​through the bindings)
    • If the values ​​are incorrect, the user should be asked to choose whether to fix the record (leave the window open) or close the window and discard the changes.

Simple enough. NSNumberFormatter should already cover most of the task and, along with the bindings, this should be pretty easy to achieve.

Problem number 1:

, , NSTextField , - . - ? - NSFormatter?

№ 2:

NSTextField "done" , Cocoa NSTextField. OSX, .

, [window makeFirstResponder:nil] "done" , NSTextField , , .

№ 3:

. (, ) NSTextField "done" , , NSTextField , .

, "" , , , .

? / ?

+4
1

1 -control:didFailToFormatString:errorDescription: , , -control:didFailToValidatePartialString:errorDescription:. , , .

NSNumberFormatter -getObjectValue:forString:errorDescription: -isPartialStringValid:proposedSelectedRange:originalString:originalSelectedRange:errorDescription:. . , , super . , ( ..), .

2 , . , NSObjectController . "" -commitEditing, -commitEditingWithDelegate:didCommitSelector:contextInfo:. NSObjectController NSController, NSEditor. ( , ), , , .

, 3 . , , , . , , , "" , .

+1

All Articles