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;
}
Related
I have a UITextField called place, it's text property contains an NSString. I would like to clear the existing content from the text property when the user taps into the text field.
I tried the code above, but nothing happened. I also tried it with the placeholder property, but it was the same. Do you have any idea what could be the problem? I think it should work.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.place.text = nil;
}
2, version - in this case nothing appears in the text field from the beginning
- (void)viewDidLoad
{
[super viewDidLoad];
...
[self textFieldDidBeginEditing:place];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.text = nil;
}
There is a property clearsOnBeginEditing you can set to YES programmatically or there is a checkbox in Interface Builder.
If you also want to clear your placeholder
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.placeholder = nil;
}
Double check that the UITextField self.place has it's delegate set to your view controller and validate that textFieldDidBeginEditing is being called.
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've a UITableViewController which has Save button. At present I'm displaying row's of UITableViewCell's. UITableViewCell contains textfield . Let's say if user enters text in 1st table cell and hits "Save" then I'm losing the entered/modified text.
suppose I have a textField
((UITextField*)[cell viewWithTag:101]).placeholder = #"title";
((UITextField*)[cell viewWithTag:101]).text = arrayActivities[indexPath.row][#"title"]; //arrayActivities[indexPath.row];
((UITextField*)[cell viewWithTag:101]).keyboardType = UIKeyboardTypeDefault;
how I would save arrayActivites editable values??
Have you tried making the UITableViewController a UITextViewDelegate? So in your tableView's .h file:
UITableViewController <UITextFieldDelegate>
and in your .m file you should be able to call:
- (void)textFieldDidEndEditing:(UITextField *)textField {
[array addObject: textfield.text];
}
Keep one global dictionary dctGlobal in your class.
Set textfield.delegate=self in cellForRowAtIndexPath method.
And then get and save value for text field in following method
- (void)textFieldDidEndEditing:(UITextField *)textField
[dctGlobal setObject:textField.text forKey: textField.placeholder];
On save, Use dctGlobal for saving into database.
Thanks!
short version: How can I make a UITextField box remove all content on the users first keypress? I don't want the info removed until the user starts typing something. ie, clearing it on begin edit is not good enough.
long version: I have three UITextField that loop around (using the return key and catching the press in the "shouldReturn" method. There is text already in the UITextField, and if the user doesn't type anything and just goes to the next UITextField, the value should stay (default behaviour).
But I want it that if the user starts typing, it automatically clears the text first. Something like having the whole field highlighted, and then typing anything deletes the fiels and then adds the user keypress.
"Clear when editing begins" is no good, because the text is immediately cleared on the cursor appearing in the field. That's not desired. I thought I could use the placeholder here, but that doesn't work as a default, and I can't find a default value property. The Highlighted and Selected properties don't do anything in this regard either.
There is a delegate method called
textFieldDidBeginEditing:(UITextField*) tf{
tf.startedEdinting = YES;
}
textFeildDidEndEditing: (UITextField*) tf {
tf.startedEditing = NO;
}
Add startEditing in a category to UITextField.
Then if value changes clear the field:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.startEditing){
textField.text = string;
} else {
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
}
}
You can add the property to the UITextField category in the following way:
.h
#property (nonatomic, assign) BOOL startEditing;
.m
#dynamic startEditing;
- (void) setStartEditing:(BOOL)startEditing_in{
NSNumber* num = [NSNumber numberWithBool:startEditing_in];
objc_setAssociatedObject(self, myConstant, num, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL) startEditing{
NSNumber* num = objc_getAssociatedObject(self, myConstant);
return [num boolValue];
}
Declare a BOOL variable in your .h file like.
BOOL clearField;
And implement the delegate methods like:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
clearField = YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
clearField = NO;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
clearField = NO;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(clearField)
{
textField.text = #""
clearField = NO;
}
}
I want to thank people for their answers, I implemented both of the main methods described here and both worked flawlessly. But I have since come across a much simpler, nicer answer and involves only one line of code :)
In the textField's didBeginEditing method, place [self.textField selectAll:self]; or [self.textField selectAll:nil];
The original answer I found had selectAll:self but this shows the cut/copy/paste menu. If you send nil instead of self the menu doesn't appear.
Adding this one line of code highlights the text on entering the textField (so gives the user a visual cue), and only removes everything once a key is pressed.
Another solution that fulfils the same purpose is by simply using a text field placeholder which is defined as:
The string that is displayed when there is no other text in the text field.
So as soon as the user starts typing, the placeholder text disappears.
That's something you can set from the storyboard, or programmatically. (Yes it took me two hours trying to figure it the harder way.. when the solution was literally one line change of code).
If you want to clear the text one the user interacts with it, there is an option in interface builder to where you can set the text field to "Clear when editing begins."
Try to use the following method.
- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
if(isFirsttime==YES)
{
textfield.text==#"";
isFirsttime=NO;
}
return YES;
}
Declare and initialize a NSString variable for your textField's initial text
NSString *initialText=#"initial text";
Then implement methods:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField.text isEqualToString:initialText)
{
textField.text=#"";
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField.text isEqualToString:#"")
{
textField.text=initialText;
}
}
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;
}
}