(Iphone app) i would like to send one message after user select textfield, in that textfield only. but when ever we click on textfield keyboard appears.
Set the delegate of the UITextField and implement this method in the delegate:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Do whatever you want
return NO;
}
Add UITextFieldDelegate in .h file and in .m file set Your yourTextField.delegate = self; and write this bellow code..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// write your messagehere
return NO;
}
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;
This question already has an answer here:
Issue related to textfield and datepicker
(1 answer)
Closed 6 years ago.
I have two textFields. On click, the first one is showing a qwerty keyboard and the second one is showing picker.
My issue is when I'm clicking on my first textField, the keyboard is showing properly. Now, After I type anything, I directly want to click on the second textField which is the showing picker and the keyboard should disappear, but the keyboard is still there.
I want to hide the keyboard as soon as I click on the second textField.
There are two ways to achieve this -
First: Use the UITextFieldDelegate.
For that-
List the UITextFieldDelegate in your view Controller's protocol list like,
#interface ViewController : UIViewController <UITextFieldDelegate>
Then make your ViewController conform to the Textfield's delegate to implement the methods like textFieldDidBeginEditing, and textFieldDidEndEditing:
So, go to the viewDidLoad method and conform to the UITextField's protocol like-
- (void)viewDidLoad
{
[super viewDidLoad];
self.firstTextField.delegate = self; //assuming your first textfield's
//name is firstTextField
//Also give your first textfield a tag to identify later
self.firstTextField.tag = 1;
}
Now, you are set to implement the delegate methods. But, to achieve your target first, you need to take a UITextField instance to know when you are typing in the firstTextField. So, declare a property of UITextField type. Do that in the interface file like-
#property(nonatomic, strong) UITextField *currentTextField;
Now in the textFieldDidBeginEditing delegate method, assign the firstTextField instance to the currentTextField when you start typing in it like-
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField.tag == 1){
self.currentTextField = textField;
}
}
After that in the textFieldDidEndEditing method, check if it is the current textfield from which you are coming out and dismiss the keyboard like-
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag == 1){
[self.currentTextField resignFirstResponder];
}
return YES;
}
Second: You can use UIResponder. As ViewControllers inherit from UIResponder, you just override the method- touchesBegan:withEvent method, something like-
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];// this will do the trick
}
In this case, when you click out side the textField, the keyboard should automatically disappear.
use UITextFieldDelegate set delegate to self in ViewDidLoad and then put this code
func textFieldDidEndEditing(textField: UITextField)
{
yourtextfieldname.resignFirstResponder()
return true
}
Step 1 :- Code for ViewController.h :-
//add UITextFieldDelegate just after UIViewController
#interface ViewController : UIViewController<UITextFieldDelegate>
Step 2 :- Code For ViewController.m :-
//inside viewdidLoad write following code, where txtInput is outlet for input text field
_txtInput.delegate = self;
// Last Thing write following code just above #end
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
//that's it use this very simple way
I'm working on an Objective-C Xcode project in Xcode 6. I have a textfield and a button that checks what's in the textfield when pressed.
However, I would like the code to run when enter key is pressed instead of the submit button.
I have seen that you can set the key equivalent for a button in Xcode however this option doesn't appear to feature in Xcode 6.
Implement textField delegate method.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self callYourCodeWantToExecute];
//return YES; // want to hide keyboard
//return NO; // want keyboard
}
You need to:
Set delegate for textField
In your delegate object handle -(BOOL)textFieldShouldReturn:(UITextField *)textField i.e. do what you need to do when return pressed and return YES if you want to text field get return NO if you don't want to handle return for text field.
First make your file delegate for UITextField and then add this method to your code ..
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Follow the below steps.
Set delegate of UITExtfield
Add tag value if there are multiple textfield
Now call the delegate method of UITextfield same as below.
Swift
optional func textFieldShouldReturn(_ textField: UITextField) -> Bool
Hope this helps!
There is view with 2 UITextField and login button.
When user is in password textbox keyboard is shown with return key set as "Go".
How i can set that return key to make action from Login button so user don't need to close keyboard and touch Login button?
Thanx in advance for reply.
it's simple
youPasswordtextField.delegate = self ; // in viewDidLoad or any suitable place
in your controllers .h file conform to UITextFieldDelegate protocol
3.implement delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField // this method get called when you tap "Go"
{
[self loginMethod];
return YES;
}
-(void) loginMethod
{
// implement login functionality and navigate user to next screen
}
You could use the TextField Delegate method as below:-
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(self.passwordTextField isFirstResponder){
[self.passwordTextField resignFirstResponder]; //Resign the keyboard.
[self loginMethod]; //call your login method here.
}
//Below case when user tap return key when done with login info then we move focus from login textfield to password textfield so as not making user to do this and for ease of user.
else{
[self.loginTextField resignFirstResponder];
[self.passwordTextField becomeFirstResponder];
}
return YES;
}
Also, don't forgt to set delegate as below along with setting UITextFieldDelegate in yourClass.h
self.passwordTextField.delegate = self;
self.loginTextField.delegate = self;
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..