Re: -stringValue on NSTextfield does not reflect actual contents [SOLVED]
Re: -stringValue on NSTextfield does not reflect actual contents [SOLVED]
- Subject: Re: -stringValue on NSTextfield does not reflect actual contents [SOLVED]
- From: Philip Dow <email@hidden>
- Date: Sun, 23 Jul 2006 10:19:58 +0200
A suggestion was made off list that I add a [title validateEditing]
before requesting the text field's contents. This did the trick. As
the docs say:
- (void)validateEditing
Validation sets the object value of the cell to the current contents
of the cell’s editor (the NSText object used for editing), storing it
as a simple NSString or an attributed string object based on the
attributes of the editor.
For anyone else that comes across this problem, you must call this
method before closing the window, as the field editor belongs to the
window. I stuck it after the modal code but before ending the sheet:
[NSApp beginSheet:sheetWin modalForWindow:[self window]
modalDelegate: nil didEndSelector: nil contextInfo: nil];
result = [NSApp runModalForWindow:sheetWin];
[title validateEditing];
[NSApp endSheet:sheetWin];
[sheetWin close];
Thanks Justin!
-Phil
On Jul 22, 2006, at 10:31 PM, Philip Dow wrote:
I am building a window in code. It contains a text field and two
buttons, one "OK" the other "Cancel." The window does not use any
bindings and I am running it modally.
The window comes to the fore when the user drags an item onto the
relevant object. The dragged item contains a string which is
initially displayed in the text field. The user can edit that
string value.
If the user presses return when finished editing, calling -
stringValue on the text field returns the correct value. However,
if the user clicks the "OK" button, calling -stringValue returns
the field's initial value rather than the edited version. I'm
stumped as to why it does this.
The code that builds the text field follows:
NSTextField *title = [[NSTextField alloc] initWithFrame:NSMakeRect
(20,60,219,22)];
[title setStringValue:defaultTitle];
[title setSelectable:YES];
[title setEditable:YES];
[title setBezeled:YES];
[title setDrawsBackground:YES];
[title setBezelStyle:NSTextFieldSquareBezel];
[title setAutoresizingMask:NSViewWidthSizable];
[[title cell] setScrollable:YES];
[[title cell] setControlSize:NSRegularControlSize];
[title setFont:[NSFont systemFontOfSize:[NSFont
systemFontSizeForControlSize:NSRegularControlSize]]];
The "OK" button is made to respond to a Return key with [sheetWin
setDefaultButtonCell:[okay cell]];
I grab the text field's string with returnValue = [title stringValue];
Setting an action and target on the text field or setting the
sendsActionOnEndEditing value makes no difference. I'm sure I just
need to add one line of code somewhere, but I don't know what it is.
-Phil
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden