Iterate through controls in iPhone - iphone

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;
}

Related

access string names using variables in FOR statement

is is possible to access objects using variables in a FOR statement?
Say i have declared:
UIImageView *UIImageView0;
UIImageView *UIImageView1;
UIImageView *UIImageView2;
and i have 3 objects in an array and i call a FOR statement if x in the array is equal to 2 i want it to add the value of x to the UIImageView name like UIImageView1 etc
I have tried:
for (int x=0; x<[theArray count]; x++) {
UIImageView[x].image = etc....
}
but it gives me a error on UIImageView[x]
subscript requires size of interface 'UIImageView'
any ideas? or is it even possible with a UIImageView?
Thanks
You don't have three elements in an array, though; you have three independent variables with similar names. If you created an actual array, containing the values of the three variables, then you could use the for loop -- and in fact, the syntax would be just as you've shown (using the actual name of the array variable, of course.)
You could say
UIImageView * views[3] = {UIImageView0, UIImageView1, UIImageView2};
and then use, for example, views[i].image in your loop.
In order to put your UIImageView into an array you need to create an instance NSMutableArray (for instance)
The above code does not show any array, instead you have three ivars
UIImageView *UIImageView0;
UIImageView *UIImageView1;
UIImageView *UIImageView2;
and to access those you would use the name, not an array.
If you however put them into an NSMutableArray you can access them using
NSMutableArray array = [[NSMutableArray alloc]
initWithObjects:UIImageView0, UIImageView1, UIImageView2, nil];
[array objectAtIndex:i ]; // where i is 0,1 or 2
...
[array release];
You can put your UIImageView to another array, and get the UIImageView from that array.
NSArray *imagesArray = #[imageView0, ...]
for(size_t i = 0; i<theArray.count; i++)
{
UIImageView *imageView = [imagesArray objectAtIndex:i];
}
Or you can use the "tag" property of UIView like:
UIImageView *image0 = ...;
image0.tag = 100; //or 0 or something else
[self.view addSubview:image0];
And then get the UIImageView by tag in your FOR statement:
for(size_t i = 0; i<theArray.count; i++)
{
UIImageView *imageView = [self.view viewWithTag:100+i];
}

NSArray of UITextFields, how can i find a specific one?

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];

Variable IBOutlet name?

Is it possible to have a variable outlet name?.
For example you have 10 labels (perhaps seats on bus). each has an outlet, seat1 seat2 etc.
Is it possible to have a for loop
that concatenates #"seat" to the increment integer. So that I can access seat1, seat2 outlet without having to specify it individually.
This doesn’t work but makes it a bit clearer what I am trying to achieve.
int i;
for (i = 0; i < [seatarray count]; i++)
{
[#”seat” stringByAppendingString[ i stringValue]] = #””;
}
Starting iOS4 you can use IBOutletCollection which allows to connect multiple instances to a single outlet which represents an array of objects, e.g. IBOutletCollection which can store UILabels only:
#property (nonatomic, retain) IBOutletCollection(UILabel) NSArray *seats;
It might be easier to simply create the array yourself at load time:
self.seatarray = [NSArray arrayWithObjects:seat1, seat2, ..., seatN, nil];
You should be able to do that with key-value coding, something like (untested code)
for (int i = 0; i != 10; ++i) { [self setValue:#"foo" forKey:[#"seat" stringByAppendingFormat:#"%d", i]]; }

Retrieve NSNumber From Array

I am relatively new to Objective C and need some array help.
I have a plist which contains a Dictionary and an NSNumber Array, with more arrays to
be added later on.
NSMutableDictionary *mainArray = [[NSMutableDictionary alloc]initWithContentsOfFile:filePath];
NSArray *scoresArray = [mainArray objectForKey:#"scores"];
I need to retrieve all the values from the array and connect them to 10 UILabels which
I've set up in interface builder. I've done the following to cast the NSNumber to a String.
NSNumber *numberOne = [scoresArray objectAtIndex:0];
NSUInteger intOne = [numberOne intValue];
NSString *stringOne = [NSString stringWithFormat:#"%d",intOne];
scoreLabel1.text = stringOne;
This seems a very long winded approach, I'd have to repeat the 4 lines above ten times to retrieve all the array values. Could I use a for loop to iterate through the array with all of the values converted to Strings at the output?
Any info would be greatly appreciated.
// create NSMutableArray* of score UILabel items, called "scoreLabels"
NSMutableArray *scoreLabels = [NSMutableArray arrayWithCapacity:10];
[scoreLabels addObject:scoreLabel1];
[scoreLabels addObject:scoreLabel2];
// ...
NSUInteger _index = 0;
for (NSNumber *_number in scoresArray) {
UILabel *_label = [scoreLabels objectAtIndex:_index];
_label.text = [NSString stringWithFormat:#"%d", [_number intValue]];
_index++;
}
EDIT
I'm not sure why you'd want to comment out _index++. I haven't tested this code, so maybe I'm missing something somewhere. But I don't see anything wrong with _index++ — that's a pretty standard way to increment a counter.
As an alternative to creating the scoreLabels array, you could indeed retrieve the tag property of the subviews of the view controller (in this case, UILabel instances that you add a tag value to in Interface Builder).
Assuming that the tag value is predictable — e.g., each UILabel from scoreLabel1 through scoreLabel10 is labeled with a tag equal to the values of _index that we use in the for loop (0 through 9) — then you could reference the UILabel directly:
// no need to create the NSMutableArray* scoreLabels here
NSUInteger _index = 0;
for (NSNumber *_number in scoresArray) {
UILabel *_label = (UILabel *)[self.view viewWithTag:_index];
_label.text = [NSString stringWithFormat:#"%d", [_number intValue]];
_index++;
}
The key to making that work is that the tag value has to be unique for the UILabel and must be something you can reference with -viewWithTag:.
The code above very simply assumes that the tag values are the same as the _index values, but that isn't required. (It also assumes the UILabel instances are subviews of the view controller's view property, which will depend on how you set up your interface in Interface Builder.)
Some people write functions that add 1000 or some other integer that allows you group types of subviews together — UILabel instances get 1000, 1001, and so on, and UIButton instances would get 2000, 2001, etc.
try using stringValue...
scoreLabel1.text = [(NSNumber *)[scoresArray objectAtIndex:0] stringValue];

Loop through labels iPhone SDK

Ok I have 8 labels and I want to loop through them but am having no luck.
This is what I have tried.
for (int i; i = 0; i < 10; i++)
{
double va = [varible1.text doubleValue] + i;
int j = 0 + I
label(j).text= [[NSString alloc]initWithFormat:#"%2.1f", va];
}
This errors out. My labels are named like this label0, label1, label2
Any help would be appreciated.
label(j) is NOT equivalent to label0, label1, etc.
You should create an NSArray of labels, then you can access them with [arrayOfLabels objectAtIndex:j]. If you're not sure what this means, please read the documentation about NSArray...
You should maybe add all your labels to a C array, probably in -viewDidLoad
UILabel* labels[] = { label0, label1, label2, ... };
(not entirely sure about the syntax)
and then access them like
labels[i].text = ...
By the way, I think you're leaking memory here:
labels[i].text = [[NSString alloc]initWithFormat:#"%2.1f", va];
initWithFormat: will return a string with a retain count of 1. labels[i].text will retain that value again. You should release the string after setting the label's text. I'd probably just autorelease it here:
labels[i].text = [[[NSString alloc]initWithFormat:#"%2.1f", va] autorelease];
or use stringWithFormat (which returns an autoreleased string):
labels[i].text = [NSString stringWithFormat:#"%2.1f", va];
for (UILabel *lbl in self.view.subviews)
{
[lbl setFont:[UIFont fontWithName:#"AppleGothic" size:22]];
}
it will change all the labels in your ViewController by just giving tags to labels.
If you cannot or do not want to put your labels in an array, you could iterate through the UIViews using the tag field as an index. You store the index numbers in them (either through IB or programatically) and then get each label using: (UIView *)viewWithTag:(NSInteger)tag.
See below (set theView to the view your labels reside in):
for (int i; i = 0; i < 10; i++)
{
double va = [varible1.text doubleValue] + i;
UILabel * label = [theView viewWithTag: i];
label.text= [[NSString alloc]initWithFormat:#"%2.1f", va];
}