So i've got a dat picker setup as a UITextField's inputView, all is working well. The problem however is that when you select the text field (and the picker shows up), you're still able to type in letters (on a simulator using computer's keyboard). How can I set it up so that when you tap on the text field and the date picker shows up, you cannot enter any additional text?
You can code your UIViewController to implement the UITextFieldDelegate protocol, assign the delegate for textField and then write the following function in your view controller :-
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
Related
When my application launches, the first NSTextField is being selected like this:
I can edit the NSTextField fine, but when I press enter to end the editing, the text becomes selected again, and the editing does not end.
I followed the Apple tutorial here, and I had the same problem with the text field being perpetually highlighted.
How do I stop this? I would like it so the text field is not the first responder of the app so it's not edited right away, and when it is being edited, clicking outside of the text field will end it. I'm not sure where to put the [[textField window]makeFirstResponder:nil] to stop the editing in the latter case.
I'm running Yosemite 10.10.2.
Your text field is selecting the text, due to the default implementation of becomeFirstResponder in NSTextField.
To prevent selection, subclass NSTextField, and override becomeFirstResponder to deselect any text:
- (BOOL) becomeFirstResponder
{
BOOL responderStatus = [super becomeFirstResponder];
NSRange selectionRange = [[self currentEditor] selectedRange];
[[self currentEditor] setSelectedRange:NSMakeRange(selectionRange.length,0)];
return responderStatus;
}
The resulting behavior is that the field does not select the text when it gets the focus.
To make nothing the first responder, call makeFirstResponder:nil after your application finishes launching. I like to subclass NSObject to define doInitWithContentView:(NSView *)contentView, and call it from my NSApplicationDelegate. In the code below, _window is an IBOutlet:
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[_menuController doInitWithContentView:_window.contentView];
}
The reason your field is getting focus when the application starts is because the window automatically gives focus to the first control. It determines what is considered first, by scanning left to right, top down (it scans left to right first, since a text field placed at the top right will still get focused). One caveat is that if the window is restorable, and you terminate the application from within Xcode, then whatever field was last focused will retain the focus state from the last execution.
I am using IB, there's a property on NSTextField called Refuses First Responder. Ticking that will prevent the highlighting of the text field immediately after the window is presented. There's some more detailed info about Refuses First Responder in this question.
No need to subclass. Simply set refusesFirstResponder = YES;
NSTextField *textField = [NSTextField new];
textField.refusesFirstResponder = YES;
That's it! Do that and it won't highlight the text in the field.
At the moment, I trigger a method on 'Did End On Exit' in my app (I'm aware that this may not be the greatest way of doing it but I'm very new to Objective C and Xcode for that matter and I'm simply doing what feels comfortable to me).
This method resigns the firstResponder from the current text field and applies it to a later text field.
The problem I'm facing is that the keyboard covers the next text field so that the use has no idea where the focus is and therefore what they are required to type.
How do I get it so that my keyboard shifts down and actually shows the text box that is currently active? Making something the firstResponder simply doesn't do what I want it to, unless there's part of the implementation I'm missing.
Here's my simple method:
- (IBAction)firstNameNext:(id)sender {
[firstNameTextField resignFirstResponder];
[surnameTextField becomeFirstResponder];
}
Any advice would be super.
Add UIScrollView in your main view then all contents as subview to UIScrollView
Now when specific UITextField needs to be able to visible in view use its delegate like this:
Note: add UITextFieldDelegate in .h file like this
#interface yourViewController : UIViewController<UITextFieldDelegate>
Also bind with File's Owner
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
{
if(textField == yourSpecficTextField) //one u want move upwards
{
yourScrollView.contentOffset = CGPointMake(0,200); //required offset
}
... //provide contentOffSet those who needed
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
yourScrollView.contentOffset = CGPointMake(0,0); //make UIScrollView as it was before
}
If you have keyboard input fields that will be covered by the virtual keyboard, then you need to move those fields out from under the virtual keyboard.
The normal way to do this is to have the controller's view be a scrollable view like UIScrollView. Moving Content That Is Located Under the Keyboard gives a very robust way of adjusting your scroll view and ensuring the required field shows.
I have tried this solution to hide keyboard but it doesn't work...
How to hide the keyboard when i press return key in a UITextField?
Many thanks
assuming you are using UITextField, how do you create the textfield? is it by xib? or by code? make sure you implement UITextFieldDelegate in your class
#interface YouClass : UIViewController <UITextFieldDelegate>
if its xib, connect your textField to the file owner's delegate. and also connect your File owner to your IBOutlet UITextField
if its by code. just do
yourTextField.delegate = self;
now implement
-(BOOL)textFieldShouldReturn:(UITextField *)textField
Just resign as first responder in your input text field, when the return key pressed. The keyboard will dismissed automatically.
You need to set the Return button to actual built in function type. In one of my apps I have set it as a "Done" button because when the user has finished they can press this and it will drop the keyboard away. To set this add this to your code in somewhere like the -(void)viewDidLoad; method.
textFieldName.returnKeyType = UIReturnKeyDone;
That should then give you a blue done button on the keyboard that hides it.
I have two TextFields and I am designing a custom keyboard for ipad.
How can I dissmis system keyboard and let mine apear.
how can I know that the user is activating which TextField?
Setting the delegate to textfields to your view controller instance you have to implement this method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
and if you return YES it will open the system keyboard, if you return NO it won't.
Before returning NO, of course, you can show your view.
You will know which textField is "pressed" using tag property.
I followed the tutorial:
http://www.neoos.ch/news/46-development/54-uikeyboardtypenumberpad-and-the-missing-return-key
to dismiss the number pad,
this tutorial add the button as sub view to the number pad,
my problem is, in the same view I am using the text field to enter text also,so, how to differentiate the number field, and text field. so that I can hide the button view accordingly.
Yoy can use UITextFieldDelegate Protocol instead of NSNotifications, and inside methods textFieldDidBeginEditing: and textFieldDidEndEditing: check which field is being used. Something like this:
- (void) textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.passwordField) {
// add subview...
}