OK button behaves differently when mouse-clicked - cocoa

I have a modal sheet with an NSTextField control, an OK button and a Cancel push button. The OK button is bound to an action method called theSheetOK in my controller class. I also bound NSTextField control to an NSString member named foo in my controller (File's Owner) and I use key-value bindings to read the text value user entered (i.e. model-key path of text field in the bindings inspector is set to foo).
All works fine if the text is entered and user hits the OK button via keyboard. When I trace the value of foo with NSLog in theSheetOK handler I see the value I just typed in the text field.
However when I clicked the OK button with mouse, the value of foo is logged as empty, also as soon as I click the OK button, the text field control grabs the focus and the text I typed appears selected. Any ideas what went wrong?
#interface MyController : NSWindowController {
#private
NSString *foo;
}
#property (copy, readwrite)NSString* foo;
-(IBAction) theSheetOK:(id)sender;
-(IBAction) theSheetCancel:(id)sender;
#end
...
#import "MyController.h"
#implementation MyController
#synthesize foo;
-(IBAction) theSheetOK:(id)sender
{
NSLog(#"theSheetOK");
NSLog(#"foo= %#", foo);
...
NSWindow* theSheet = [self window];
[NSApp endSheet:theSheet returnCode: NSOKButton];
[theSheet orderOut:nil];

Sometimes you need to press enter to "confirm a change" to cocoa bindings. I'm not sure, but it's possible that when you hit enter both the change and button action are performed.
If that's the case, select your NSTextField and mark the option "Continuously Updates Value" so things get synced properly.

Related

Change NSTexfield focus by pressing Enter key

In my OS X App, how can I change the NSTextField focus by pressing the Enter key instead of pressing the Tab key?
You may use NSText - (void)textDidEndEditing:(NSNotification *)aNotification delegate method, where you should use NSWindow’s makeFirstResponder: to change the current first responder. See the NSResponder class reference for details on this.
First set delegate of your firs textField to self. Then in same class:
- (void)textDidEndEditing:(NSNotification *)aNotification {
NSLog(#"delegate method");
[self.window makeFirstResponder:[[[aNotification object]window]nextResponder]];
}
I assume that you do this in AppDelegate class.

Enabled binding for NSTextField bound to NSString

#interface MyClass {
NSString *_myString;
}
#property (copy) *myString;
#end
#implementation MyClass
#synthesize myString = _myString;
- (void)awakeFromNib {
self.myString = #"";
}
In the nib, I have an NSTextField and an NSButton. The text field's Value binding is set to myClass.myString. I verified that the variable _myString is being updated correctly when text is typed into the text field.
The Enabled binding of the NSButton is set to myClass.myString.length. However, when I start up the program, the NSButton is enabled! If I go to the textfield and type something into it, the button stays enabled. Then if I erase the text from the textfield, the button becomes disabled.
But why isn't the button disabled to begin with, after the call in awakeFromNib ? Do I have to do some additional work to make the binding work in the opposite direction (myClass.myString --> NSTextField) ? I thought that declaring myString as a property would do the trick.
I'm not sure what is causing this but I would say the easiest way to fix it, since your text will start out empty, is in awakeFromNib do something like [myButton setEnabled:false];

Show NSPopover from NSToolbarItem Button

I want to show an NSPopover from an NSToolbarItem button in my toolbar.
(i.e. positioned below the button).
Ideally, I want to pass the NSView of the button to the popover to position it.
My question is, how do I get the NSView of the NSToolbarItem?
[toolbarbutton view] always returns nil.
The answer appears to be in the video for the 2011 WWDC Session 113, "Full Screen and Aqua Changes." Basically, put an NSButton inside the NSToolbaritem and use the view of that.
A blog post is here: http://www.yellowfield.co.uk/blog/?p=33, and a sample project is on github at http://github.com/tevendale/ToolbarPopover
All in the sprit of http://xkcd.com/979!
You can send the action directly from the NSButton enclosed in the NSToolbarItem (which is what you should generally do anyways, consider segmented controls, where each segment has its own target/action), and that will do the trick.
Instead of getting the view from the IBAction sender, connect an IBOutlet directly to the toolbar item and use that to get the relative view:
In your header file:
#property (weak) IBOutlet NSToolbarItem *theToolbarItem;
#property (weak) IBOutlet NSPopover *thePopover;
In your implementation file, to show the popover:
[self.thePopover showRelativeToRect:[[self.theToolbarItem view] bounds] ofView:[self.theToolbarItem view] preferredEdge:NSMinYEdge];
This will also work for showing popups from menu item selections inside a toolbar item.
While I did achieve that the Popover was shown using the approach mentioned by Stuart Tevendale, I did run into problems when I tried to validate (enable / disable) the NSToolbarItems using the NSToolbarDelegate:
-(BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem {
BOOL enable = YES;
NSString *identifier = [toolbarItem itemIdentifier];
// This does never get called because I am using a button inside a custom `NSToolbarItem`
if ([identifier isEqualToString:#"Popover"]) {
return [self someValidationMechanism];
}
// For this the validation works when I am using a standard `NSToolbarItem`
else if ([identifier isEqualToString:#"StandardToolbarItem"]){
return [self someOtherValidationMechanism];
}
return enable;
}
So I would advise not to display a Popover from NSToolbarItem. An alternative might be to show a Page Sheet: How to show a NSPanel as a sheet

Execute an action from a text field ONLY when enter is pressed [duplicate]

I have two NSTextFields: textFieldUserID and textFieldPassword.
For textFieldPassword, I have a delegate as follows:
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
This delegate gets called when textFieldPassword has focus and I hit the enter key. This is exactly what I want.
My problem is that controlTextDidEndEditing also gets called when textFieldPassword has focus and I move the focus to textFieldUserID (via mouse or tab key). This is NOT what I want.
I tried using controlTextDidChange notification (which is getting called once per key press) but I was unable to figure out how to detect enter key ( [textFieldPassword stringValue] does not include the enter key). Can someone please help me figure this one out?
I also tried to detect if textFieldUserID was a firstResponder, but it did not work for me. Here is the code I tried out:
if ( [[[self window] firstResponder] isKindOfClass:[NSTextView class]] &&
[[self window] fieldEditor:NO forObject:nil] != nil ) {
NSTextField *field = [[[self window] firstResponder] delegate];
if (field == textFieldUserID) {
// do something based upon first-responder status
NSLog(#"is true");
}
}
I sure could use some help here!
If I understood you correctly, you could set an action for the password text field and tell the field to send its action only when the user types Return. Firstly, declare and implement an action in the class responsible for the behaviour when the user types Return on the password field. For example:
#interface SomeClass …
- (IBAction)returnOnPasswordField:(id)sender;
#end
#implementation SomeClass
- (IBAction)returnOnPasswordField:(id)sender {
// do something
}
#end
Making the text field send its action on Return only, and linking the action to a given IBAction and target, can be done either in Interface Builder or programatically.
In Interface Builder, use the Attributes Inspector, choose Action: Sent on Enter Only, and then link the text field action to an IBAction in the object that implements it, potentially the File’s Owner or the First Responder.
If you’d rather do it programatically, then:
// Make the text field send its action only when Return is pressed
[passwordTextFieldCell setSendsActionOnEndEditing:NO];
// The action selector according to the action defined in SomeClass
[passwordTextFieldCell setAction:#selector(returnOnPasswordField:)];
// someObject is the object that implements the action
[passwordTextFieldCell setTarget:someObject];
[passwordTextFieldCell setTarget:self];
[passwordTextFieldCell setAction:#selector(someAction:)];
- (void) someAction{
//handle
}

NSTextField enter key detection or firstResponder detection

I have two NSTextFields: textFieldUserID and textFieldPassword.
For textFieldPassword, I have a delegate as follows:
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
This delegate gets called when textFieldPassword has focus and I hit the enter key. This is exactly what I want.
My problem is that controlTextDidEndEditing also gets called when textFieldPassword has focus and I move the focus to textFieldUserID (via mouse or tab key). This is NOT what I want.
I tried using controlTextDidChange notification (which is getting called once per key press) but I was unable to figure out how to detect enter key ( [textFieldPassword stringValue] does not include the enter key). Can someone please help me figure this one out?
I also tried to detect if textFieldUserID was a firstResponder, but it did not work for me. Here is the code I tried out:
if ( [[[self window] firstResponder] isKindOfClass:[NSTextView class]] &&
[[self window] fieldEditor:NO forObject:nil] != nil ) {
NSTextField *field = [[[self window] firstResponder] delegate];
if (field == textFieldUserID) {
// do something based upon first-responder status
NSLog(#"is true");
}
}
I sure could use some help here!
If I understood you correctly, you could set an action for the password text field and tell the field to send its action only when the user types Return. Firstly, declare and implement an action in the class responsible for the behaviour when the user types Return on the password field. For example:
#interface SomeClass …
- (IBAction)returnOnPasswordField:(id)sender;
#end
#implementation SomeClass
- (IBAction)returnOnPasswordField:(id)sender {
// do something
}
#end
Making the text field send its action on Return only, and linking the action to a given IBAction and target, can be done either in Interface Builder or programatically.
In Interface Builder, use the Attributes Inspector, choose Action: Sent on Enter Only, and then link the text field action to an IBAction in the object that implements it, potentially the File’s Owner or the First Responder.
If you’d rather do it programatically, then:
// Make the text field send its action only when Return is pressed
[passwordTextFieldCell setSendsActionOnEndEditing:NO];
// The action selector according to the action defined in SomeClass
[passwordTextFieldCell setAction:#selector(returnOnPasswordField:)];
// someObject is the object that implements the action
[passwordTextFieldCell setTarget:someObject];
[passwordTextFieldCell setTarget:self];
[passwordTextFieldCell setAction:#selector(someAction:)];
- (void) someAction{
//handle
}

Resources