NSDatePicker becomeFirstResponder - objective-c

I have a data entry form where the first field the user wants to enter is an NSDatePicker.
I wish this field to be highlighted when the user clicks a button (labelled 'New')
I have tried putting in [myDatePicker becomeFirstResponder] but this has no effect.
Can anybody show me how to set the focus to myDatePicker?

Unfortunately, becomeFirstResponder does not work that way.
As mentioned in the docs, you should send makeFirstResponder: to the window that contains the control you wish to become the first responder. In your case the window that holds your NSDatePicker.
becomeFirstResponder can be overridden in a control to return false if you do not want it to become the first responder.

Is your picker linked to a textfield for the output? I have linked custom pickers to my textfields keyboard property before. If the output is set to the textfield, you can make the textfield the first responder and the picker will come up in place of the keyboard.

Related

UIPicker with UITextField

I made a research and all posts here are very blury regarding this issue.
I would like to use a UIPicker when pressing on a UITextField.
I would realy appriciate a step by step guide.
I tryd all posts here but every post gives me only a portion of what I need and I can't seem to connect it all together.
This is the last part of my application and i'm going crazy to finish it..
Thank you in advanced!
Gal
There is an inputAccessoryView property that contains a view that will appear instead of a keyboard on the bottom of the screen. Create a UIPicker, adjust its frame, provide values and assign it to the inputAccessoryView property.
UIPicker will appear when user taps on your UITextField.
If you don't need editing, you may use a UILabel instead of the UITextField. Solution is the same. I have a ready-made class if you need.
Here's a way:
http://www.youtube.com/watch?v=t_F1ex5opgA&t=14m10s
-(BOOL)textfieldShouldBeginEditing:(UITextField *)textField
where textField is the name of your text field.
Call your UIPickerView and return NO so that your picker is loaded rather than the keyboard.
The idea is to call an action that opens the UIPicker when the user taps the UITextField. Because the UITextField does not responde to the usual touchUpInside events that UIButtons respond to, I would just overlay a transparent UIButton on top of the UITextField and just in case, make the text field's userInteractionEnabled property NO. Hook the UIButton to responde to touchUpInside and call a method that opens the UIPicker. Another option would be an immediate response to the text field's touch by implementing "textFieldShouldBeginEditing" and immediately resigning the text field.
The next step would be to present the UIPicker - if we are talking about iPad, this would best be done by using a UIPopoverController. On iPhone, maybe consider bringing it up modally. When you create the view controller that holds this UIPicker, be sure to add a delegate property to it so that whatever value that was selected on the picker can be transfered back to the main view controller and on to the UITextField.
Hope this helps with getting you started.

NSTextField simulate return pushing a button

I have an NSTextField with value bound to a Shared User Default Controller
The NSUserDefault value change only when user press "Enter" ok keyboard.
I'd like to produce the same effect (simulate the return hit on a specific field) when user push a button on the UI.
Is it possible ?
In your bindings, you can check the "Continuously Updates Value" box to have the user defaults update as you type, so if your button method just checks the value, it will always be up to date. A more general solution to update the value of a text field is to have the window make something other than the text field the first responder (this is what happens when you tab out of a text field to the next responder). You can do this with:
[window makeFirstResponder:window];
In your button's action, do this:
[[NSUserDefaultsController sharedUserDefaultsController] commitEditing]

NSSearchField as first responder

guys! I have a NSSearchField in a window's toolbar. I tell the window to set its first responder to the search field: it works. Now: if I ask the window to tell me what's its first responder, it is NOT the search field even if the search field has the focus...
Is there a way to set the search field as the window's first responder without having this mismatch?
Thank you
No. Or at least not easily. When a search field, or any simple text field, has keyboard focus, it isn't actually the first responder. When an instance of NSTextField (NSSearchField's superclass) is asked to become first responder, it asks the window for a "field editor", which is another text field used exclusively for editing. This editor is placed within the text field and set as the actual first responder.
To get the text field or search field, you could go up the view hierarchy using superview on the first responder. (I think it is the field editor's direct superview.) I'm pretty sure the actual text field is also set as the field editor's delegate, so you could also get it that way, but I don't remember where I read that.
More information about the field editor is available through the NSWindow class reference.

Getting UIPicker to appear when user selects a UITextField

I have a simple UITextField called month where I get users to simply enter the month they want via the keyboard that comes up. I would now like it for them to be able to use a UIPickerDate (or UIPicker) to make this selection instead. So when they press on the text field, a mini UIPicker appears and they make there selection, press anywhere on the screen and the picker disappears.
Does anyone know how to do this or has any suggestions? I am pretty new to programming and have looked at other answers but everyone seems to be referring to this being done in a table.
Thanks in advance!
You can set the inputView property on the UITextField to be an instance of UIDatePicker. When the instance of UITextField becomes the first responder, the picker view will be displayed with the standard keyboard animation.
// Assume that self.monthTextField and self.datePicker
// are properties of the view controller class
self.monthTextField.inputView = self.datePicker;
As for dismissing, that depends on the context. If there are more text fields to populate, consider adding a UIToolbar as the inputAccessoryView of self.monthTextField. Then you can add something like a UIBarButtonItem to make the next text field the first responder, similar to how the standard keyboard provides a Next button.

NSTextField setEnabled

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?
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.
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].