I know this is a basic question but i´m a little confused, so i hope you can help me. I have a tableview with multiple dynamic tableview cells, and inside each tableviewcell i have multiple textfields. Each cell has a different tag and also the textfields and i want to access the uitextfields values as you can imagine. My problem is, i´m not using IBoutlet for the textfields (it would be a enormous amount of IBoutlets)...I´m using - (void)textFieldDidEndEditing:(UITextField *)textField...but i just can´t seem to make the correct connections in the IB, this is my code:
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == [self.view viewWithTag:102]) {
[textField resignFirstResponder];
}
After this, do i have to connect the respective UItexfield (and all textfields) to self? and then, do i have to use the editing did end event?...
Regards
I guess that the answer to this question is another question: What do you want to do with the text that the user enters?
I assume that you have some kind of data model that you want to store the data in.
If so, then when this function is called, you need to take the text that is already in the textField and save it to your data model immediately as it is entered.
For instance, you can access the text that was entered like this:
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == [self.view viewWithTag:102])
{
[textField resignFirstResponder];
yourDataModel.stringToSave = textField.text;
}
}
Related
I'd like to achieve the following behaviour of a single UITextField and the keyboard:
when the view has loaded UITextField becomes first responder and the keyboard opens (so far so good):
- (void)viewDidLoad
{
[self.editField becomeFirstResponder];
self.editField.delegate = self;
}
Now user inputs some text and presses the return key. That text is added to the data source in the following method:
- (IBAction)didEndOnExit:(id)sender {
//add self.editField.text to data source
}
Now after the return key is pressed and the above UITextField's method gets called and executed I would like the UITextField to clear, the cursor to be placed and be visible at the beginning of the text field and the keyboard not to hide so that new item could be entered in the textfield and added to the data source.
This is how I return to the previous view in the app (using a button):
- (IBAction)backButtonPressed:(id)sender {
[self dismissViewControllerAnimated:NO completion:^{}];
}
I tried playing with UITextFieldDelegate's methods and UITextField's becomeFirstResponder and resign FirstResponder, but am unable to achieve the above described behaviour. I've seen posts here on Stack Overflow about using consecutive UITextField's to enter data, but not to use the same UITextField time and again.
You should override the below mentioned method and do whatever you want and showing the keyboard too.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Have a try :)
While dismissing view controller all other code executions freezes when (view controller is being dismissed).
Use some delay (performselectorafterdelay). or
completionhandler of dismissingviewcontroller for executing your code when viewcontroller is being dismissing and
Also execute code on main Thread which includes UI changes or updates
after saved your data.. you have to make the uitextfield to be nil... like self.editField.text = nil. then you can call [self.editfield becomesfirstresponder].. after you will add more items to array or else anything everytime the field should be nil after you added..
From the Doc
Return Value: YES if the text field should implement its default
behavior for the return button; otherwise, NO.
For EX - Use the Delegate of UITextField :
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//add self.editField.text to data source
textField.text = #"";
return NO; // This will do your need
}
Because I used UITextField's action method - (IBAction)didEndOnExit:(id)sender the keyboard always got closed. Apparently that method makes the UITextField resign from being first responder. Instead of adding data to data source in that method, it can be added in the delegates's method - (BOOL)textFieldShouldReturn:(UITextField *)textField and so the keyboard won't close.
I have a readonly text field. I want that when the value is changed to this field , to set another text field also.
Which event should I use. tried with editingChanged , but it's not getting called.
pls help.
If the text field is readonly, then you must set the text programmatically, right? In that case, when you set the text, set the text of the other text field at the same time.
If I misunderstood your question, and the user edits the textfield, you can listen for changes this way:
[textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventValueChanged];
And handle the callback the following way:
- (void)textFieldDidChange:(UITextField *)textField
{
// Handle change
}
From Apple documentation
textFieldShouldBeginEditing: Asks the delegate if editing should begin
in the specified text field.
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
Parameters textField - The text field for which editing is about to
begin.
Return Value YES if an editing session should be initiated; otherwise,
NO to disallow editing.
Discussion When the user performs an action that would normally
initiate an editing session, the text field calls this method first to
see if editing should actually proceed. In most circumstances, you
would simply return YES from this method to allow editing to proceed.
Implementation of this method by the delegate is optional. If it is
not present, editing proceeds as if this method had returned YES.
So use something like:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
BOOL editable;
if (textField == myReadOnlyTextField) {
editable = NO;
} else if (textField == myEditableTextField) {
editable = YES;
} else {
// editable = YES/NO/Other Logic
}
return editable;
}
Also in order for the delegate methods to get called your interface should conform to the UITextfieldProtocol: add this to your .h file <UITextFieldDelegate> so it looks like this:
#interface ViewController : UIViewController <UITextFieldDelegate>
Implement this delegate method if you are implementing in ios.
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange{}
If you are implementing in macos then use below delegate method:-
-(void)controlTextDidChange
I got 2 textField and when the user types something in, and pushes the return button or back button on my navigation controller, that it saves the textFields. I've already tried this:
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
NSLog(#"textFieldShouldEndEditing");
return YES;
}
but this doesn't print out anything. The names of my two textFields are self.CellOne.textField and self.CellTwo.textField and I've also added the <UITextFieldDelegate> delegate in my class. What I actually want is whenever the foces goes off the textFields, it needs to be saved.
I have a one page application with a couple of text fields. One field is a Drop-down menu, with which the user can choose the item to fill in this text field (text1). After this text field (text1) is filled, how can I automatically update other text fields (text2, text3, etc).
For example: I have three text fields, text1, text2, text3. On the backend, I have a matching array list between them, so the value from text1 will able to find the proper value for text2 and text3. But, how can I dynamically update text2 and text3 after the user changes the value of text1.
Another more basic question: how can I display the text string in text field in viewdidload?
For example: I have retrieved value from database, how to display this value in text field upon load of the view.
ok, I'll try to address the questions one by one:
first the displaying of a text field on load.
create a outlet to a label to hold your text.
example - start in .h file
#property (weak, nonatomic) IBOutlet UILabel *textbox1;
Now in Interface Builder, drag out a label and then connect the label you just dragged with the property you setup.
Now you have an outlet to store you value.
now to put something in it go to .m file in -(void)viewDidLoad add something like this:
self.textbox1.text = #"some text to display";
That should get something on the screen.
Now, if you want to dynamically update other on-screen labels either during or after the user is typing, you need to implement a text field delegate. Its a little complicated to try and explain the whole thing, but basically in your .h file your implementation line should look like this:
#interface YourViewControllerNameHere : UIViewController <UITextFieldDelegate>
then in your .m file you have a few changes to make
first in viewDidLoad add this line
self.textbox1.delegate = self; //this tells the UILabel to send changes to your program
then in some open space you can implement these callback methods:
then run your program and the logging will tell you when and where things are happening.
basically, you call your code from the correct method depending on what you want to do.
- (void)textDidChange:(id<UITextInput>)textInput {
NSLog(#"text did change");
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"text field did begin editing");
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(#"text field did end editing");
}
-(void)textWillChange:(id<UITextInput>)textInput {
NSLog(#"text will change");
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(#"text field should change characters in range");
//if this is disabled no character will appear
//use to filter out bad characters
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"text field should return");
return YES;
}
I have a few UITextFields (within UITableViewCells) on my UIView with a "Save" UIButton. I want to do some basic validation on the UITextFields when the user clicks the "Save" button.
I have overridden textFieldDidEndEditing to save each of my UITextField data to an instance variable; however, if a user clicks the save button before either clicking the "Return" button of the UIKeyboard or clicking on another UITextField the data in my last UITextField is never saved to my instance variable and validation always fails.
I am looking for a way to trigger an "onBlur" (I know that's a JS thing)-type event to save my string in UITextField to my instance variable.
I've looked through the UITextFieldDelegate Protocol and I do not see anything like this.
Is there a method I may be missing?
to trigger textFieldDidEndEditing on your UITextField, you will need to call
[_txt resignFirstResponder];
were _txt is your UITextField
Please note that if you dont have a reference to _txt and you need to find the first responder in order to resign it
You could use the solution from this question Get the current first responder without using a private API
Then instead of calling
[_txt resignFirstResponder];
you would call
[self.view findAndResignFirstResponder];
Try this
// if we encounter a newline character return
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// enter closes the keyboard
if ([string isEqualToString:#"\n"])
{
[textField resignFirstResponder];
return NO;
}
return YES;
}
which will trigger
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
// Call webservice
return YES;
}