Recognizing all objects generated by a loop - iphone

This question is a bit related to one I posed previously entitled "Recognizing UIButton from a loop".
In this case, I have generated a bunch of UITextFields with a loop. When the value of one of them is changed, I'm able to recognize which textfield it was.
However, I want to then edit every textfield that was generated. Specifically, I get input from the one textfield that was edited, and I want to recalculate the other textfields based on the input and name of the recognized textfield.
How do I call for every one of the other generated, non-edited textfields to be modified?
/Vlad

Since you are already using tags, this is what viewWithTag is used for:
// Get a reference to the textfield with tag 3
UITextField *textField3 = (UITextField *)[self.view viewWithTag:3];
// Calculate your new value
float result = 4.32; // Calculate the value that you want the textfield with tag 3 to display
// Change the contents
textField3.text = [NSString stringWithFormat:#"%f", result];

store the text fields in an array, then when you want to change the value
[self.myTextFieldArray enumerateObjectsUsingBlock:^(UITextField *textField, NSUInteger idx, BOOL *stop){
if (![textField isEqual:theTextFieldThatWasEdited])
textField.text = #"whatever text you want";
}];

Related

How to display a multiple values in textfield?

I'm new to the iPhone. I have four number buttons like 1,2,3,4. When I click any button the value is displayed in a textfield, but when I click two buttons only one value is displayed in textfield.
How to display in multiple values in textfield?
You can add new string value to already displayed value in your text field, for example, in following way:
UITextField *field; // <- reference to your UITextField
NSString *value = [[NSNumber numberWithInt:1] stringValue]; // <- value to add
field.text = [NSString stringWithFormat:#"%#%#", field.text, value];

Problem to write more than a line in a text view

i have a UITextView in which i have to write programmatically multiple lines. When i do this :
[textView setText:#"String number 1\n"];
[textView setText:#"String number 2\n"];
i get only : String number 2 as if it's writing the line over the other. I appreciate any suggestions :)
As the name suggests, setText: is a setter: it replaces the existing text.
That's quite like if you had an int val and you were doing:
val = 5;
val = 8;
Of course at the end val will be equal to 8 and the value 5 will be lost. Same thing here.
Instead, you need to create a string that contains all the lines, and affect directly to the textView:
[textView setText:#"Line1\nLine2\nLine3\n"];
If you need to do it incrementally, say you need to add text to existing text in the textView, first retrieve the existing text, append the new line, and set the new text back:
// get the existing text in the textView, e.g. "Line1\n", that you had set before
NSString* currentText = [textView text];
// append the new text to it
NSString* newText = [currentText stringByAppendingString:#"New Line\n"];
// affect the new text (combining the existing text + the added line) back to the textView
[textView setText:newText];
setText replace whole content of textView. You better to prepare result string and than set it by single setText method call.
for example:
NSString *resultString = [NSString stringWithFormat:#"%#%#", #"String 1\n", #"String 2\n"];
[textView setText:resultString];
you can append the strings :
mTextview.text = [mTextview.text stringByAppendingString:aStr];
According to How to create a multiline UITextfield?, UITextField is single-line only. Instead, use a UITextView.
In addition, as others have pointed out, if you call setText a second time, you'll overwrite the previous value. Instead, you should do something like this:
[textView setText:#"String number 1\nString number 2"]

How to generate UITextFields dynamically with different name and accessing it from another place?

I have an iPhone application in which there are dynamically generated textfields to capture a value of product quantity. The default quantity is 1. I have generated textfields like this
for(int i=0;i<[array count];i++)
{
UITextField *i=[[UITextField alloc]init];
i.frame=CGRectMake(90, Yposqtytextfield, 60, 30);
i.borderStyle=UITextBorderStyleRoundedRect;
[self.scrollview addSubview:i];
i.delegate=self;
i.text=#"1";
i.tag=i;
[appDelegate.qtyArray addObject:i.text];
}
But I want values of quantities in next page. For that I have taken this qtyarray. Now the user is allowed to change quantity. So how can I change the value of quantity in the array. as declaration of textfield is local to that loop. So at next page navigation how can i get the values of all these textfields?
UITextField *aField = (UITextField*)[appDelegate.qtyArray objectAtIndex:0]
it should be
NSString *aValue = [appDelegate.qtyArray objectAtIndex:0];
because he is adding value of textfield not the textfield object itself.
you can access your qtyArray anywhere in the application. Since application delegate is singleton class. You can access value like this
NSString *aValue = [appDelegate.qtyArray objectAtIndex:0];

Retrieve UITextField Text for a specific UITextField Tag

Hey
Just wondering how do you..
Iv created some code that automatically creates a certain amount of UITextField input by the user.
Each UITextfield has a set tag automatically created.
The user then inputs his value into each of the UITextFields.
I want to retrieve the input text from the UITextFields which correspond to the tag.
I thought I nearly had it with:
NSString * tmpV = (NSString*)[choiceTextField.text viewWithTag:result];
where result is a increment, choiceTextField is the UITextField. With this I get a problem not defining instance, which I can't do as the UITextFields are generated in code not on the xib.
So to sum up, basically want the retrieve the text from a UITextField with specific tag.
Thanks
Dan
UITextField *yourTextField = (UITextField *)[self.view viewWithTag:result];
NSString *getText = myTextField.text;
Hope this helps
Your textfields must be a subview of a parent view. You need to write the following in your view controller of the parent view:
NSString *text = ((UITextField*)[self.view viewWithTag:result]).text;
Just go with Hetal Vora's solution: looks much cleaner!
-viewWithTag can be called on any view and searches all its subviews for one that matches the tag. Thus, you will find your UITextField by calling it on any view that is above it in the hierarchy. For example, you could call it on the view controller's view that contains your text field, as follows:
NSString *tmpV = [[myViewController.view viewWithTag:result] text];
If you are already in the view controller's class you could use:
NSString *tmpV = [[self.view viewWithTag:result] text];
On second thought, the following may be more correct & will avoid any compiler errors:
UITextField *myTextField = (UITextField *)[self.view viewWithTag:result];
NSString *tmpV = myTextField.text;

How to combine the values of pickerview with textview?

In My Application pressing the submit button i wanted to perform submit operation but i don't know how combine the textview's value with pickerview's value.if it is possible then show me code with example.
How would you like to combine the values, as a string?
If you simply want to combine them in a single string, here's an example of how you can do it:
- (IBAction)submitPressed:(id)sender {
// Load the selected row for the first component in your picker view
// If you have more than one component you may need to look up more
// Also, you may need to translate this number into something more meaningful
// such as what the selected row represents
int selectedRow = [pickerView selectedRowInComponent:0];
NSString *myString = [NSString stringWithFormat:#"%# %d",
textView.text, selectedRow];
// Do something with myString...
}