I have managed to add a UIPickerView as an input view to UITextField in viewDidLoad, but now I want to make the UITextField as first responder when another textfield (with tag =4) ends editing. This is the code I'm using:
This is in the viewdidload, and all the other code needed to initialize the picker view is there:
UIPickerView *myPickerView = [[UIPickerView alloc] init];
myPickerView.showsSelectionIndicator = YES;
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.backgroundColor = [UIColor colorWithRed:76/255.0 green:76/255.0 blue:76/255.0 alpha:1];
textfieldInput.inputView = myPickerView;
This is in my text field did end editing method:
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag == 4){
[textfieldInput becomeFirstResponder];
}
The problem is that when the textfieldInput becomes the first responder, the picker view isn't shown, instead the keyboard is presented.
Also, it should be mentioned that i have another textfield on top of this that gets resigned if it became the first responder and becomes hidden. This textfield is the textfield that has a tag of 4. (This textfield has been added for design reasons).
This code works for me... are you sure that call the correct reference of textfield?
try: [self.textfield becomeFirstResponder];
Related
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;
I have view controller that contains three UITextFields called "Name", "Location" and "Birthday".
I have this code here which opens a UIDatePicker:
// Open date picker
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Make a new view, or do what you want here
UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,245,0,0)];
[self.view addSubview:pv];
return NO;
}
Here I have two problems.
Problem 1
When I tap any field the datepicker opens. I just want to open the datepicker when "Birthday" is tapped.
Problem 2
When I tap on a field, then tap again, it opens another datepicker.
How can I stop this from happening?
Peter
Instead try this on viewDidLoad:
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
birtdayTextField.inputView = datePicker; //birtdayTextField is your "birthday text field"
For opening the datepicker only for a specific textField, check the (UITextField *)textField before opening datePicker. You can set tags to each textField and then use that tag to differentiate.
For your second issue, you need to use resignFirstResponder
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..
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).
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.).