I have an UITextField to search the word that typed there. I set the returnKeyType as UIReturnKeySearch. But I couldn't figure how to bind a function that I created, to the "Search" button at the keyboard.
self.searchWord.returnKeyType=UIReturnKeySearch;
How can I do that? Thanks.
If I'm understanding correctly you need to implement the UITextfield delegate:
In your header file
#interface MyViewController : UIViewController <UITextFieldDelegate>
in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
self.searchTextField.delegate = self;
}
Then implement the delegate method.
- (BOOL)textFieldShouldReturn:(UITextField *)aTextfield {
// Only do search when user hits return key on the search textfield.
if ([aTextfield isEqual:self.searchTextField]) {
// Method that does the search.
[self doSearch];
}
return YES;
}
Related
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 have a small ios app and i need to execute a button action when user presses the 'enter'
In the apple developer site I have found this article
and my question is where should I enter this
[myButton setKeyEquivalent:#"\r"];
Try something like this:
Set self (current UIViewController) as delegate for your instance of UITextField or UITextView:
self.myTextField.delegate = self;
Don't forget to implement protocol <UITextFieldDelegate> in your controller #interface.
Then implement those methods:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.myButton performSelector:#selector(myButtonClicked:) withObject:self.myButton];
return YES;
}
- (IBAction)myButtonClicked:(id)sender
{
NSLog(#"myButton clicked");
}
I am trying to create a very basic app that has a text field and that prints out the value entered into the text field using NSLog when the return key is pressed. The code is below. So far, nothing seems to happen when text is put into the text field and I press enter. Can anyone assist?
#property (weak, nonatomic) IBOutlet UITextField *textfld;
- (void)viewDidLoad
{
[super viewDidLoad];
self.textfld.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)textFieldDidBeginEditing:(UITextField *)textfld {
}
- (void)textFieldDidEndEditing:(UITextField *)textfld {
NSLog(#"text is : %#",textfld.text);
}
http://pastie.org/9078792
http://pastie.org/9078793
Use - (BOOL)textFieldShouldReturn:(UITextField *)textField delegate method of UITextField in ViewController.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"text is : %#",textfld.text);
return YES:
}
textFieldShouldReturn: asks the delegate if the text field should process the pressing of the return button. Read more.
set a breakpoint at line 27 self.textfld.delegate = self; and see if self.textfld is nil. That will tell you whether you correctly created your property and wired your textfield to that property in Interface Builder. Chances are you didn't...
Try this:
NSLog(#"%#",_yourtextfieldname.text);
NSLog(#"%#",self.yourtextfieldname.text);
I have pickerview in my view controller. I am displaying it by adding into subview and then changing frame of subview. When my pickerview displayed I have one button behind it. When I click on those area after dispalying pickerview still that button action is called. How to set pickerview proper?
U subclass the PickerView like below will help u to fix this issue.
//
// WPCustomPickerView.h
// test
//
// Created by VASANTH K on 08/01/14.
//
//
#import <UIKit/UIKit.h>
#interface WPCustomPickerView : UIDatePicker
#end
implementation file
//
// WPCustomPickerView.m
// test
//
// Created by VASANTH K on 08/01/14.
//
//
#import "WPCustomPickerView.h"
#implementation WPCustomPickerView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
id hitview=[super hitTest:point withEvent:event];
if(!hitview)
{
if(point.y<=self.frame.size.height&&point.y>=0)
return self;
}
return hitview;
}
#end
Here i override the hitTest to make UIPickerView response for the user interaction. This is the way how apple make main picker view transparent for user touch by return nil when user directly touch on the main view instead of picker content.
(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;
}