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.
Related
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.
I know there are some questions about how to show a toolbar above keyboard.
I read some of them. But, I could not make the toolbar which have TextView like message app. I want to make a toolbar which is showed when button tapped.
I also use IB to create a toolbar because I need some adjustment to appearance.
The code now I make is below.
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIToolbar *ibToolbar;
// this text view is subview of ibToolbar
#property (weak, nonatomic) IBOutlet UITextView *barTextView;
#end
- (void)viewDidLoad {
[super viewDidLoad];
[self.ibToolbar removeFromSuperview];
self.barTextView.inputAccessoryView = self.ibToolbar;
}
- (IBAction)buttonPushed:(id)sender {
[self.barTextView becomeFirstResponder];
}
When I tap the button, I want to look at toolbar above keyboard to put text on textView.
But, nothing happened when I push the button.
I think that why this code does not work, also because the viewController's view does not parent view of the toolbar,so [self.barTextView becomeFirstResponder] can not be effective.
I think that something else which I did is needed to work the things.
If anyone know do the things work, please tell me.
I am trying to set up undo and redo for each textfield and unsure how to figure out how to determine which text field is the first responder.
Is there an argument I can pass into the methods called by the buttons from the toolbar, or do I need to do some fancy footwork?
This is an idea:
If the viewController becomes delegate of each textField, then the viewController will get notified as each textField's value changes, or becomes first responder.
To adopt the delegation, you will do:
#interface MyViewController : UIViewController <UITextFieldDelegate>
#end
#implementation
- (void)someMethod{
// for a series of textfields
myTextfield1.delegate = self;
myTextfield1.delegate = self;
// or you hook the delegate in IB
}
// then you get notified
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// textField here that gets passed in as an argument is the first responder
// if you have, let's say tag number for each
NSInteger activeTextFieldTag = textField.tag;
}
#end
Here is the reference to UITextFieldDelegate Protocol
I'm making an application that requires the use of multiple textFields with number pads as there first responder. I have created an image to use as a negative button that will be an addition to the number pad.
I am wondering if there is a way to check which textField the number pad is typing to.
Any help would be appreciated!
If all of your potential first responders are UITextFields, another approach would be to conform your controller class to UITextFieldDelegate protocol, and then grab a reference to the currently editing UITextView at the time it begins editing.
Conform your class in your .h:
MyController : NSObject <UITextFieldDelegate> //Might often be a UIViewController rather than an NSObject subclass...
Define a property:
#property (weak, nonatomic) UITextField *editingField;
Then synthesize in your .m:
#synthesize editingField = __editingField
Then implement:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self setEditingField:textField];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self setEditingField:nil];
}
Now, whenever you want to know which text field is your first responder:
UITextField *firstResponder = [self editingField];
If there are only a limited number of them, you can query each with [textFieldN isFirstResponder]. If you want a general purpose utility, you can look at each subview in a view and see whether it is the first responder or whether any of its subviews farther down the hierarchy are.
Hopefully somebody can help me out here. I have a UIView with 3 text fields. I want the "return" key from the keyboard to resign the first responder, regardless of which text field the users curser is in. My code works for 2 of the 3 text fields, but consistently (as in always) it refuses to resign first responder from the 3rd text field, and I don't know why (the text fields are identical. The field that does not work is the 3rd field...
Here is the code if it helps
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[firstTextField resignFirstResponder];
[secondTextField resignFirstResponder];
[thirdTextField resignFirstResponder];
return YES;
}
Here is the property declaration in the H
#property (weak, nonatomic) IBOutlet UITextField *firstTimeTextField;
#property (weak, nonatomic) IBOutlet UITextField *secondTextField;
#property (weak, nonatomic) IBOutlet UITextField *thirdTextField;
It seems pretty straight forward, so I'm wondering if there is some kind of error checking that I should be doing to determine whats happening here.
The file is declared as a UITextFieldDelegate as well...
Thanks for the time and the assistance!
Have you set the delegate to ALL UITextFields?
Set the delegate to all UITextFields.