I have four UITextFields and associated each with an outlet in .h files. I have also defined 4 dismiss function for each textfield as shown below:
- (IBAction)dismiss1:(id)sender;
- (IBAction)dismiss2:(id)sender;
- (IBAction)dismiss3:(id)sender;
- (IBAction)dismiss4:(id)sender;
#property (strong, nonatomic) IBOutlet UITextField *name;
#property (strong, nonatomic) IBOutlet UITextField *email;
#property (strong, nonatomic) IBOutlet UITextField *weight;
#property (strong, nonatomic) IBOutlet UITextField *age;
Implementation of dismiss function in the .m file:
- (IBAction)dismiss1:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)dismiss2:(id)sender {
[sender resignFirstResponder];
}
...
I am very sure that the outlet is connected to each UITextFiled correctly. The IBAction of each dismiss function is also connected with 'Editing did end' event correspondingly. However, when I ran the app using simulator, the keyboard will not dismiss when I click 'Enter/Done'. It's also very weird that when I place the breakpoint inside the dismiss function, clicking 'Enter' when typing in the corresponding UITextField does not bring up the debugger.
Thanks a lot for helping!
Update: I checked the object type of sender (dismiss1) using breakpoint and it's UITextFiled. However, I did not entered the debugging mode when I click 'Enter' in the first TextField, but entered the debugging mode when I click on the second TextField (before typing).
set UITextFielDelegate
textfieldname.delegate = self;
use this code it will resolve your issue
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Use the code to resign keyBoard without specify textFiled name.
- (IBAction)dismiss1:(UITextField*)sender {
[self.view endEditing:true];
// or use [sender resignFirstResponder];
}
Add text field delegate
name.delegate = self;
email.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
No need to create four actions for textfield
You can set delegate of Textfield and use following method
- (void)textFieldDidEndEditing:(UITextField *)textField
Inside this method you can compare your textfield with method returning textfield and get you task done in "if" condition
Hope it help you!!
Assign the tag to different textfields
then >
textfield.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textfield.tag==num)
{
[textField resignFirstResponder];
}
return YES;
}
The Editing did end is called when a textfield did lose the "focus", mean it is not the first responder any more (because it has resigned or an other one became first responder).
Use textFieldShouldReturn to detect when the return key is called.
And call resignFirstResponder on the texfield when this is called. The Editing did end event will occur after that.
Edit : you have to implement something (Your ViewController?) as delegate of the textfield.
Related
In my app, I have a view controller which is having a search bar with UItextfield at the top and a UIImageview below that. The image view is initially hidden.
I want this image view to unhide through an if statement. The user will enter keywords into the textfield and when a certain word will match a pre defined string in the.m file, then it must show the image.
I originally had two view controllers but now I added another one (thirdviewcontroller). As I enter a word into the textfield, the simulator will direct me back to the code highlighting in green on this line:
if ([string1 isEqualToString:string2]) {
locationMap.hidden = YES;
This is .h file:
#interface ThirdViewController : UIViewController
{
IBOutlet UITextField *searchLocation;
IBOutlet UIImageView *locationMap;
}
-(IBAction)SearchGo;
#end
This is the .m file:
-(IBAction)SearchGo{
NSString *string1 = searchLocation.text;
NSString *string2= #"sydney";
if ([string1 isEqualToString:string2]) {
locationMap.hidden = YES;
}
}
It sounds like you've accidentally set up a breakpoint. Simply remove the breakpoint by clicking the blue arrow to the left of the line it breaks on.
In viewDidLoad method, use:
locationMap.hidden = YES;
In your -(IBAction)SearchGo method, use:
locationMap.hidden = NO;
OR for your searchGo method:
-(IBAction)SearchGo{
if ([searchLocation.text isEqualToString:#"sydney"]) {
locationMap.hidden = NO;
}else {
//implementation
}
}
I am guessing, you have attached the IBAction with your textfield,searchLocation and triggered the action specifying "Touch up Inside". This will not work for couple of reasons.
First of all, you need to implement the textFieldShouldReturn: delegate method, so that your controller knows when you press return, it should hand over the control from your text field. Then again, a you have attached your action method to your text filed, as soon as you tap on the textfield, it goes to your method and start comparing but at this point, you have typed nothing in your textfield and it fails to conform to your if condition.
the solution is to either use the have a button and attach the action method to that button. That way, after you have typed the word "sydney", and you hit on the button. It will take whatever in your textfield and compare to that.
Here is the solution-
See the extra button named "Go". Attach your method to it.
This is my .h file-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextFieldDelegate>
#property(nonatomic, strong) IBOutlet UITextField *searchLocation;
#property(nonatomic, strong) IBOutlet UIImageView *locationMap;
-(IBAction)SearchGo:(id)sender;
#end
And this is the .m file-
#import "ViewController.h"
#interface ViewController ()
#property(nonatomic, strong) NSString *string1;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark- textfield delegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
self.string1 = textField.text;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(IBAction)SearchGo:(id)sender{
NSString *string2= #"Sydney";
if ([self.string1 isEqualToString:string2]) {
self.locationMap.hidden = NO;
}
}
#end
Save the string from the textfield after your editing is done through the delegate method. Make sure, you attach the UITextFieldDelegate to your ViewController.
Alternatively, you may want to avoid all this trouble and use the UISearchDisplay controller.
I have a text field named "fieldPassword" declared as an IBOutlet
#property (strong, nonatomic) IBOutlet UITextField *fieldPassword;
I synthesize it later on and then, in an attempt to have the return key dismiss the keyboard, I have:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
fieldPassword.delegate = self;
[fieldPassword resignFirstResponder];
[self.view endEditing:YES];
return YES;
}
The problem is that when I run the simulator and hit the return key in the designated text field, nothing happens. Previously, I also had fieldPassword.delegate = self; in -viewDidLoad and that crashed the simulator with an unrecognized selector error.
Any help is much appreciated!
It should be [self.fieldPassword resignFirstResponder]; instead of [fieldPassword resignFirstResponder];
Also self.fieldPassword.delegate = self; should be in viewDidLoad or viewDidAppear
If you don't set the delegate earlier, you won't get the delegate callback. Try this in viewDidLoad:
self.fieldPassword.delegate = self;
You might have been missing the self before.
Follow the below things
1.Go to xib or storyboard where you have set that your view.
2.Right Click the TextField.If you click that you can see the
Outlets->Delegate with Empty Circle
3.Just connect with File's Owner.It is Yellow Color.
Once you do this,the circle is not empty.It is filled now.
4.Then go to declaration or .h part
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextFieldDelegate>
5.Then in .m
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
I have few text fields in a UIViewController. For some of the text fields I have used IBOutlet to make property. For some textFields that I add programmatically (since they are in scroll view, they are not in the constraints of the view controller window in soryboard. ) , I have just made them property without IBOutlet.
So for example, I have:
#property (weak, nonatomic) **IBOutlet** UITextField *descriptionTextBox;
#property(strong , nonatomic) UITextField *cityTextField;
Now I set the delegates of both in ViewDidLoad & also in .h file <UITextFieldDelegate>
But after implementing - (BOOL)textFieldShouldReturn:(UITextField *)textField method , the keyboard only return for the text field having IBOutlet.What can I do for it?
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]; //works only for text field declared in storyBoard.
return YES;
}
In your view controller, say [self.view endEditing:YES]. This will dismiss the keyboard regardless of who is first responder.
I think it would be helpful to see more code.
However your issue could be that you're not setting the delegate on your UITextFields hence the method only being called on the one in the storyboard.
Check to see whether you have set delegate to your UITextField objects that were created programatically.
I have 1 problem when i click button "Next" of keypard to move cursor from TextField to TextView
The first ,all is ok. But in the second , the cursor jump into the line second of textView and then every time i click Next from TextField, the cursor in TextView will down one line more.
Well, the question seems to be an intresting one. In most of the cases, the cursor jump is from one TextField to another TextField. Here you need to jump the editing from one TextField to another TextView, so you can try this method:
First create outlets for your textView and TextField, in your viewcontroller.h, say
#property (strong, nonatomic) IBOutlet UITextView *textView;
#property (strong, nonatomic) IBOutlet UITextField *textField;
and viewcontroller.m do this:
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
if(textField isFirstResponder)
{
[textField resignFirstResponder];
[textView becomeFirstResponder];
}
return YES;
}
EDIT :
If the method don't work as expected, this will make the change:
'
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
if(textField isFirstResponder)
{
[textField resignFirstResponder];
[textview performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
}
return YES;
}
(To be honest, I have no idea how this performSelector with 0 delay solved the issue, but the solution worked for the OP .Thanks Luong Anh for the comment.)
you only need to put [yourtextView becomeFirstResponder];
in your textFieldShouldReturn method.
This question already has answers here:
NSMutableArray addObject not working
(2 answers)
Closed 9 years ago.
I'm trying to add UITextField input into an NSMutableArray using the following IBActions, connected to the 'did end editing' part of the UITextFields:
- (IBAction) returnKey1
{
[textInputOne addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[textInputOne resignFirstResponder];
[players addObject:textInputOne.text];
}
- (IBAction) returnKey2
{
[textInputTwo addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[textInputTwo resignFirstResponder];
[players addObject:textInputTwo.text];
NSLog(#"array: %#",players);
}
and I've initialized the players array in the viewDidLoad section like so:
- (void)viewDidLoad
{
[super viewDidLoad];
players = [[NSMutableArray alloc] init];
}
but the array remains 'nil'. Anyone know how to fix this?
UITextField doesn't send any actions when the user taps Return. So you won't receive the “did end editing” action when the user taps Return.
UITextField sends “did end editing” when it resigns first responder. You can make it resign first responder when the user taps Return by setting the text field's delegate and implementing textFieldShouldReturn:. Start by finding the class extension at the top of your ViewController.m. Edit it (or add it if it's not there) to declare that the class conforms to UITextFieldDelegate:
#interface ViewController () <UITextFieldDelegate>
#property (nonatomic, strong) IBOutlet UITextField *textFieldOne;
#property (nonatomic, strong) IBOutlet UITextField *textFieldTwo;
#end
Next, implement textFieldShouldReturn: in the class to make the text field resign first responder:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
Finally, in your xib, connect each text field's delegate outlet to the view controller (which is normally File's Owner).
I hope you have set the delegates to textfield properly. As you have allocated the players array in viewDidLoad try with following code
- (IBAction) returnKey1
{
[players addObject:textInputOne.text];
}
- (IBAction) returnKey2
{
[players addObject:textInputTwo.text];
}
Both returnKey1 & returnKey2 IBActions assigned to textfield did end editing events.
Now to resign the key board
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
I tried the same thing in one sample project it worked well.