Reputation: 559
I am trying to find a way to prevent the keyboard from appearing when the user taps on a TextField but could`t find a way to do it.
I tried this code after I linked my textField to delegate and still it did not work for me, the code was logging but the keyboard did appear.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(@"BeginEditing");
return YES;
[textField resignFirstResponder];
}
When I return with NO
I lose the focus from the textField, which I need.
The textFields I have are filled with values from a buttons on the same view, thats why I don't want the keyboard to appear, and at the same time I want the user to select the textField they want to fill.
Upvotes: 1
Views: 5884
Reputation: 73
This will help you for sure.
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
activeField.inputView = dummyView; // Hide keyboard, but show blinking cursor
return YES;
}
I tested and it is working for me. Hope this will be useful for others who have similar issue.
Upvotes: 3
Reputation: 20551
so here you just use flag int variable to assign the value to focused textfield
define int i;
flag globally in .h or .m file
after that in textField Delegate method use bellow code...
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField == yourtextField1 ) {
i=1;
}
else if (textField == yourtextField2 ) {
i=2;
}
else if (textField == yourtextField3 ) {
i=3;
}
else if (textField == yourtextField4 ) {
i=4;
}
return NO;
}
-(IBAction)yourbutton1_Clicked:(id)sender{
if( i == 1){
yourtextField1.text=yourbutton1.titleLabel.text;
}
else if ( i == 2){
yourtextField2.text=yourbutton1.titleLabel.text;
}
else if ( i == 3){
yourtextField3.text=yourbutton1.titleLabel.text;
}
else if ( i == 4){
yourtextField4.text=yourbutton1.titleLabel.text;
}
else{
NSLog(@"Please Click On TextField");//here you can put AlertView Message
}
}
and so on.......
also you can use common method with sender id of button and also tag......
Upvotes: 0
Reputation: 8109
if you just want user to select the textfield to fill and does not want to keyboard to show up then you can do the following:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { selectedTextFieldTag = textField.tag; return NO; }
use selectedTextField
value to identify which textfield to fill in your code. return NO
will not allow keyboard to appear.
Upvotes: 4
Reputation: 26812
[textField resignFirstResponder]; will not be called because you are returning from the method before it can get called. Does that not fire a warning?
Try returning NO here or if that doesn't work, try disabling user-interaction on the text field:
[myTextField setUserInteractionEnabled:NO];
Upvotes: 1
Reputation: 1271
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(@"BeginEditing");
[textField resignFirstResponder];
return YES;
}
Upvotes: 0