i loaded up an array with UITextFields, they are all created dynamically (and locally) and i need to keep reference to them for a later point.
my text fields are name, textField01, textField02, etc.
i want to pull out textField02 out and change the data. how can i search through my array of them get it out? i tried "isEqualToString" and failed. i can't use the .tag cause i'm using that for something else. i can't compare to the .text value cause i don't know what it will be (entered by user).
You could use NSDictionary here. Add each text field with a named key that you'll use for retrieval later.
If you are naming them sequentially why not just add them to an NSMutableArray
Then you can get at them by index.
NSMutableArray *textFields = [[NSMutableArray alloc] initWithCapacity:10];
UITextField *textField = nil;
for (int i = 0; i < 10; i++) {
textField = [[UITextField alloc] initWithFrame:myFrame];
[self.view addSubview:textField];
[textFields addObject:textField];
[textField release]; textField = nil;
}
Later on
UITextField *myTextField = [textFields objectAtIndex:1];
Related
i have 9 IBOutlets for imageView named imageView1,imageView2,....
i have to assign first object of array to imageView1 & second object to imageView2,.....
Now my question is- is there any way to iterate through the property name
example-
for(int i=0;i<[randomizedArray count];i++)
{
NSString *imageViewName=[NSString stringWithFormat:#"%#%i",#"imageView",i];
imageViewName.image=[appDelegate.splittedImageArray ObjectAtIndex:i];
}
that means it automatically assign value as
imageView1.image=[appDelegate.splittedImageArray ObjectAtIndex:i];
imageView2.image=[appDelegate.splittedImageArray ObjectAtIndex:i];
The closest you could get to something like that without changing the IBOutlets would be this:
NSArray *imageViews = #[imageView1,imageView2,imageView3, ...];
for(int i=0; i<[randomizedArray count]; i++) {
[[imageViews objectAtIndex:i] setImage:[appDelegate.splittedImageArray objectAtIndex:i]];
}
But there is a better solution - IBOutletCollection
You can define a property like this:
#property (nonatomic, retain) IBOutletCollection(UIImageView) NSArray *imageViews;
And connect all of your imageViews like so:
Then you will have an array containing all of your UIImageViews, and you can use the above code without having to declare such an array manually...
dont think it complexly. This is very simple. put tag for the imageviews like tag 1 for imageview1, 2 for imageview2 and so on. Then run a for loop and pick the object and assign to the array. Thats all.
Like assume you put tag from 1 to 7 (don't put duplicate tag or 0 for any of your views)
NSMutableArray *arr = [[NSMutableArray alloc] init];
for(int ii = 1; ii<=7; ii++){
UIImageView *imgView = (UIImageView *)[self.view viewWithTag:ii];
[arr addObject:imgView];
}
yourArray = arr;
[arr release];//if your array is a property otherwise no need to release.
Hope this will help you.
use NSClassFromString
for(int i=0;i<[randomizedArray count];i++)
{
NSString *imageViewName=[NSString stringWithFormat:#"%#%i",#"imageView",i];
Class theClass = NSClassFromString(imageViewName);
theClass.image=[appDelegate.splittedImageArray ObjectAtIndex:i];
}
One way I have used to do this . Created the array of IBOutlets so you can iterate through it and assign the corresponding image.
for(int i=0;i<[randomizedArray count];i++)
{
UIImageView *imageView=[imageOutlets objectAtIndex:i];
UIImage *image=[images objectAtIndex:i];
imageView.image=image;
}
I have a situation here, I want to add user name with a check button in a view, looks simple :)...
Question is I may have single or multiple user, so I need to add it through code only, I want to add all names like a ladder with its corresponding check button.
What is the best way to do that?
I have tried by adding labels for each user.. While adding I wanted to set its top position one by one to make it aligned properly.. Do I have to calculate top like this? Any easiest way is there.. Like how they are doing in Android (relative Layout).
thanks
You can create array of usernames and use following code.
This is test code for your reference.
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:#"1"];
[arr addObject:#"2"];
[arr addObject:#"3"];
[arr addObject:#"4"];
[arr addObject:#"15"];
Now looping:
for (int i = 0; i<[arr count]; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10*10*i, 100, 40)];
label.text = [arr objectAtIndex:i]; //usernames from array
[self.view addSubview:label];
}
The Objective-C equivalent of relativelayout is Cocoa Auto Layout
I would use a UItableView with a custom cell. The cell contains your input fields. This way you can have as many inputs as you like and they will always be layed out in order in the same way, and you don't need to write any funky code. You also get scrolling and memory management for free.
How can I read UITextField value in an IBAction? I'm creating UITextField programmatically. So I can't set #property and #synthesize using Xcode. The code to generate UITextField is as follows:
for(i=0; i<[fieldName count]; i++)
{
UITextField *name = [fieldName objectAtIndex:i];
frame = CGRectMake(fromLeft, fromTop, totalWidth, totalHeight);
name = [[[UITextField alloc] initWithFrame:frame] autorelease];
name.borderStyle = UITextBorderStyleRoundedRect;
name.returnKeyType = UIReturnKeyDone;
//name.placeholder = [fieldName objectAtIndex:i];
name.autocapitalizationType = UITextAutocapitalizationTypeWords;
name.adjustsFontSizeToFitWidth = TRUE;
name.keyboardType = UIKeyboardTypeDefault;
[name addTarget:self action:#selector(doneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
[scroller addSubview:name];
fromTop = fromTop + 40;
}
Now I want to read values of each textbox in a button click (IBAction). Can anyone help me please?
You could use something like this to loop through all UITextFields that are subviews of self.view and add their text to a NSMutableArray:
for (UITextField *field in self.view.subviews) {
if ([field isKindOfClass:[UITextField class]]) {
if ([[field text] length] > 0) {
[someMutableArray addObject:field.text];
}
}
}
if your doneEditing: looks like this doneEditing:(id)sender then you can say:
UITextField *field = (UITextField *)sender;
NSString *myText = field.text;
EDIT:
To access a UITextField without setting as an instance variable you need to tag it when you create it:
[textField setTag:1];
then whenever you want to access it you can get it from its parent view by the tag:
UITextField *myTextField = [scroller viewWithTag:1];
NSString *myString = myTextField.text;
in your case, set the tag to i+1 for example to have all the textfield with unique tags.
implement the IBAction function like the following:
-(IBAction) doneEditing:(UITextField*)sender
{
NSString * val = sender.text;
}
try using the UITextFieldDelegate , i think its better for your case.
add each UITextField a Tag and by that you will recognise the UITextField.
NSString *value = sender.text;
If inside an IBAction, of course.
Which ignores that there is a set of text fields, and a single button action.
The simplest solution to the overall problem would be to store a set (or array) of text fields in an instance variable, and iterate over that set in the button action. But that is a rather coarse approach; it is probably better to use the text field delegate method and store text values directly in an array, using the button to trigger the save.
In addition, Apple HIG would tell you that you should update your data model as the text fields are edited, rather than use a "Save" button - which is poor UX design - unless, of course, the values of individual fields can interact.
In my iphone app, I have created some UITextField in scrollView dynamically. And added one button in xib. On that button's TouchUpInside event, I opened UIImagePickercontroller to open photo library and taking selected image on UIImageView.
Now when modalview is dissmissed, values in my UITextField disappears.
How do I retain the UITextField values?
txt = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 30.0f, 200.0f, 30.0f)];
[txt addTarget:self action:#selector(keyDown:)forControlEvents:UIControlEventEditingDidEndOnExit];
txt.textColor = [UIColor blackColor];
txt.borderStyle = UITextBorderStyleBezel;
txt.autocorrectionType = UITextAutocorrectionTypeNo;
[fieldArray addObject:txt];
Using for loop I am adding txt to NSMutableArray fieldArray.
Here is the code where I fetch values from TextFields
NSMutableArray *insertValues = [[NSMutableArray alloc]init];
//Taking values from textFields
for (int i=1; i<=nooffields; i++) {
UITextField *tf = (UITextField *)[self.view viewWithTag:i];
NSLog(#"TF : %#",tf.text);
if (tf.text.length<=0) {
tf.text=#"";
}
[insertValues addObject:[NSString stringWithFormat:#"'%#'",tf.text]];
}
EDIT :
And also when I display values from database in this textFields and try to edit it. It gives me null values.
What may be the reason? If there any other alternatives? please help me
UITextField *textField=[[UITextField alloc]init];
[textField setFrame:CGRectMake(x,y, width, hight)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:textField];
The information that your scrollview uses to get populated should be stored outside the scrollview, in a NSMutableDictionary for example. So everytime cellForRowAtIndexPath gets called it can retrieve the information from that source. So I advise you to store the UITextField's text within the dictionary. You can index it by its tag number. That way you won't loose whatever it contained.
I hope it helps you
Cheers
I finally got a work around for my problem.
I am temporarily storing my textField's values into database when presentModalViewController is called.
These values I retrieve back into the textFields on viewWillAppear.
This may not be a very efficient solution but it worked in my case.
Hope this helps someone.
Thanks for all your responses.
I have 4 UITextFields that I'm dynamically creating, in the viewDidLoad, which works good. I want to reference those objects when the UISlider value changes. Right now I'm storing those objects in a NSMutableArray and accessing them like so from the sliderChanged method:
NSInteger labelIndex = [newText intValue];
labelIndex--;
NSUInteger firstValue = (int)0;
NSMutableArray *holeArray = [pointsArray objectAtIndex:labelIndex];
UITextField *textField = [textFieldArray objectAtIndex:firstValue];
NSString *newLabel1Text = [[NSString alloc] initWithString:[[holeArray objectAtIndex:firstValue] stringValue]];
[textField setText: newLabel1Text];
[newLabel1Text release];
Everything is working good, but the program crashes on the setText: method. The last message I get from the program is: [UILabel drawTextInRect:] and then I get a EXC_BAD_ACCESS failure.
I want to be able to acces that dynamically created UITextField, but I must be going about it the wrong way.
Thanks!
Uh, yea, you create a text field, but you aren't displaying the field itself, just creating it.
If you want to do what I think you want to do, I would just do if statements.
ex.
if (firstValue == 1)
{
fieldone.text = #"whatever";
}
else if (firstValue == 2)
{
fieldtwo.text = #"whatever";
}