In my app I have a view that is a form that has quite a few inputs.
When the UITextField calls textFieldDidBeginEditing, it checks the tag and will bring up a UIPopoverController or the keyboard depending on the what the input is meant to be.
If the keyboard is up, I need it disappear when the user presses a textfield that brings up the popover. However I cannot make it disappear, I have tried every way to get rid of the keyboard but it just stays there. I have tried:
calling resignFirstResponder in textFieldDidEndEditing
calling [self.view endEditing:YES] in textFieldDidEndEditing
calling resignFirstResponder AND [self.view endEditing:YES] in textFieldDidBeginEditing checking for the previous tag is equal to a keyboard input text field.
Any ideas would be great.
I have ripped it out and and put it in a example project if anyone wants to see the exact behaviour.
http://dl.dropbox.com/u/61692457/KB_Test.zip
Declare a Global UITextField in .h file
UITextField *txtfld;
Replace Your method textFieldDidBeginEditing with textFieldShouldBeginEditing and now write this code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == 1 || textField.tag==3)
{
if(numPickerPopover == nil)
{
numPicker = [[[NumPicker alloc] initWithStyle:UITableViewStylePlain] autorelease];
numPicker.delegate = self;
numPickerPopover = [[UIPopoverController alloc] initWithContentViewController:numPicker];
[numPickerPopover setPopoverContentSize:CGSizeMake(60.0, 260.0f)];
}
[numPickerPopover presentPopoverFromRect:textField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[txtfld resignFirstResponder];
return NO;
}
if (textField.tag == 2)
{
txtfld = textField;
return YES;
}
return YES;
}
To dismiss the keyboard when the user touches the textField that brought it up, add this method:
- (IBAction)dismissKeyboard:(id)sender {
[textField resignFirstResponder];
}
In Interface Builder, connect this method to the textField event you want, like touch up inside (or whatever is more appropriate).
Related
How can I disable the keyboard when I touch-up inside a UITextField?
What I want to do is show a custom digital keyboard instead of the default one.
If I understand correctly that you are looking to create a custom keyboard in the app, I don't think we need to disable the default keyboard when we touch-up inside a UITextField.
We just need to create a custom view and assign it to the inputView property of the UITextField to replace the default keyboard.
For example, something like this:
yourTextField.inputView = yourCustomKeyboardView
See more here.
Note: For Objective-C (Xcode)
In your viewDidLoad: set delegate for textfields which you want to disable.
self.textfield.delegate = self;
and insert this delegate function:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == yourTextfiledOutletInstance) {
[self showCustomkeyboard];
return NO;
}
return YES;
}
//Show custom keyboard
-(void)showCustomkeyboard{
// Handle your operation here to show custom keyboard
}
set the UITextField delegate in the ViewController class, and then add this method in the class
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
Use resignFirstResponder to dismiss your keyboard.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
set inputView on the UITextView to the custom view you want to be used in place of the system keyboard.
myTextView.inputView = myCustomView;
I cant seem to find this answer for the manually clearing the UISearchBar with a backspace, only with a cancel button click. The code below hides the keyboard when the clear button is clicked, but so does the backspace to an empty UISearchBar. Id like to leave the keyboard open in that scenario since someone might be typing something else.
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
[self filterData: text];
if(text.length == 0)
{
[searchBar performSelector:#selector(resignFirstResponder) withObject:nil afterDelay:.1];
}
}
Your code includes if(text.length == 0) and that means that when the size of the input becomes zero, keyboard is dismissed. However the actual piece should be as follows:
- (BOOL) searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
return YES;
}
My best try on such an issue is to do as suggested here BUT IT DOES NOT WORK WHEN YOU CLICK CLEAR BUTTON :( but I thought it might help you though:
https://stackoverflow.com/a/16957073/1465756
Add a tap gesture on your whole view:
- (void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
and dismissKeyboard when the view is tapped:
- (void) dismissKeyboard
{
[self.searchBar resignFirstResponder];
}
I understand you want to dismiss the keyboard when the user hits Cancel, and you also want to dismiss when the user hits the clear (x) button.
You do not need to implement searchBar:textDidChange:.
To detect cancellation, implement searchBarCancelButtonClicked:. Here you can trigger resignFirstResponder.
To detect clear, adopt UITextFieldDelegate and implement textFieldShouldClear:. This will be called after the user taps clear, but before the clear occurs. Return YES to allow the clear to occur. You may dismiss the keyboard before returning.
I have designed a keyboard on my app Now I have the query is that whenever somebody tap on the textfield the keyboard on the device appear which i don't want
is there any way to disable the keyboard so that the user can see the keyboard designed on this app.
In your ViewController add the delegate of the UITextField :
#interface ViewController : UIViewController <UITextFieldDelegate>
While Creating the UITextField or in ViewDidLoad:
self.myTextField.delegate = self;
In your implementation file:
#pragma mark - UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
Note that you can set the return of textFieldShouldBeginEditing to NO or YES according to your needs, in your case you may return YES , but provide the logic of displaying your custom keyboard
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
And Needs to add delegate <UITextFieldDelegate>
In this method you also can write code to show your custom keyboard.
lol
Set the inputView to a zero sized view:
textField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
You can use following approach to restrict keyboard display when user tap on it :
Method : 1
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
This method hide the Keyboard and dont show the cursor.
Method : 2
UIView* HideView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)] autorelease];
textField.inputView = HideView;
This method hide the Keyboard, but Cursor is appear.
You can disable user interaction.
textField.userInteractionEnabled = NO;
In iPhone, I have a view which has a UITextField. When I tap on the clear button of UITextField's the keyboard dismissed instead of clearing the text in the UITextField. On an iPad it is working correctly. What can I do to fix this?
Just clear the field, resignFirstResponder (if you want to hide keyboard) and return NO/false
Note: set Attributes inspector property of UITextField
Clear Button -> Appears while editing
so it will display the clear button while editing in the text field.
// Objective-C
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = #"";
[textField resignFirstResponder];
return NO;
}
// Swift
func textFieldShouldClear(textField: UITextField) -> Bool {
textField.text = ""
textField.resignFirstResponder()
return false
}
Try this code after you attach delegate of uitextifield
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
return true;
}
First, check all the code blocks that related to your UITextField (especially the code yourTextField.hidden = YES;)
Put break points and analyze every UITextField delegates that you implemented.
(textFieldDidEndEditing,textFieldShouldEndEditing,textFieldShouldReturn.etc.)
OR
Implement the textFieldShouldClear delegate and write the code here to visible and clear your UITextField
To do this, you have to set the clearButtonMode as below,
yourTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
yourTextField.delegate = self;
//For active keyboard again
[yourTextField becomeFirstResponder];
Then implement the textFieldShouldClear delegate
YourClass.h
#interface className : UIViewController <UITextFieldDelegate>
YourClass.m
-(BOOL)textFieldShouldClear:(UITextField *)textField {
yourTextField.hidden = NO;
yourTextField.text = #"";
return YES;
}
Just make sure U've given these two
editingTextField.delegate = self;
editingTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
TextFieldShouldClear is needed only if you need to do some customizations :-)
Are you doing some thing in this method?
Maybe you are are calling resignFirstResponder in this delegate method, thats why the keyboard is getting dismissed.
Please go through the delegate methods, and check what u r doing exactly.
This issue happened also if you have
yourTextField.clearButtonMode = UITextFieldViewModeNever;
Check this line and delete it or change view mode..
Instead of showing the keyboard I want to display a popover view when a textField is selected (my code is at the bottom). If the keyboard isn't showing then everything works great. However, if the keyboard is showing and then the textfield is selected the keyboard doesn't get dismissed, somewhere the firstResponders must be getting lost but I don't know where. Does anyone have a solution to this?
My textfield:
self.startDateTextField = [[UITextField alloc] initWithFrame:CGRectMake(79, 148, 138, 27)];
[self.startDateTextField setBorderStyle:UITextBorderStyleRoundedRect];
[self.startDateTextField setDelegate:delegate];
[self.startDateTextField addTarget:delegate action:#selector(editStartDate:) forControlEvents:UIControlEventEditingDidBegin];
[popoverWrapper addSubview:self.startDateTextField];
and in editStartDate: I have:
-(void)editStartDate:(UITextField *)textField {
[textField resignFirstResponder];
DatePickerVC *datePickerVC = [[DatePickerVC alloc] init];
datePickerVC.delegate = self;
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:datePickerVC];
[self.popoverController setDelegate:self];
[self.popoverController presentPopoverFromRect:CGRectMake(0, 0, 5, 5) inView:textField permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
}
This is very easy to do, use your UITextFieldDelegate methods specifically UITextFieldShouldBeginEditing and return NO and execute the code to show the popover instead. This way the keyboard is never shown to begin with.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self.view endEditing:YES]; // added this in for case when keyboard was already on screen
[self editStartDate:textField];
return NO;
}
for it to work make sure you set the delegate of the textField to self (the view controller) and in your editStartDate method remove the resignFirstResponder call.
Try like this in your editStartDate: method
[self.startDateTextField resignFirstResponder];
EDIT:
But instead of doing resign the keyboard when you click in textfield, you can make something like setInputView for Textfield to bring out the popViewController.
DatePickerVC *datePickerVC = [[DatePickerVC alloc] init];
datePickerVC.delegate = self;
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:datePickerVC];
[self.popoverController setDelegate:self];
[self.popoverController presentPopoverFromRect:CGRectMake(0, 0, 5, 5) inView:textField permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
self.startDateTextField = [[UITextField alloc] initWithFrame:CGRectMake(79, 148, 138, 27)];
[self.startDateTextField setBorderStyle:UITextBorderStyleRoundedRect];
[self.startDateTextField setDelegate:delegate];
self.startDateTextField.inputView = self.popoverController;
You appear to be resigning the first responder of the text field; however, this isn't necessarily the first responder, which may explain why calling it has no effect.
Instead, you should use the endEditing category to recurse through all children of your view to resign the first responder from whichever view it is attached to:
[self.view endEditing:YES];
In any case, as you never want to show the keyboard, you can simply implement UITextFieldDelegate to override the default behaviour.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Your popover code.
return NO;
}
UITextFieldDelegate Protocol Reference.
So you're trying to hide the keyboard immediately after the text field is selected and display something else?
There's two things I can think of:
Give the text field some time to get it together before resigning the first responder:
[textField performSelector:#selector(resignFirstResponder) withObject:nil afterDelay:0.5];
Set the inputView property of the text field to nil or a UIView with a clear background color.
If inputView on the text field doesn't get you what you want, you can also simply put an invisible button on top of the UITextField and just show the popover from the button's action. To the user it will appear as if the text field brought up the popover. If at that point the keyboard is still there, call resignFirstResponder on all possible first responders (text fields etc.).