How can I increase numeric part of property name in for() loop? - iphone

I've 10 pan recognisers and want to assign to class property.
How can I increase numeric part of property name in for() loop?
for (int i=0; i < [_myArray count]; i++)
{
myClassInstance.recognizer = pangesture + i ?? // doesn't work of course. but how??
}

It's not fantastic form — definitely follow Caleb's comments wherever possible — but if you're really backed into a corner:
for(int i = 0; i < [_myArray count]; i++)
{
NSString *nameOfProperty = [NSString stringWithFormat:#"pangesture%d", i];
UIPanGestureRecognizer *recogniser = [self valueForKey:nameOfProperty];
}
That's using key-value coding; IBOutlets are necessarily KVC compliant or there'd be no way to load the NIB.

I've 10 pan recognisers and want to assign to class property. How can
I increase numeric part of property name in for() loop?
I'm not sure I understand your question fully, but assuming that you've got 10 gesture recognizers named g1 through g10 that you want to assign to 10 objects using a loop, a good approach would be to put those 10 gesture recognizers into an array and then make the assignment using the current index:
NSArray *recognizers = #[g1, g2, g3, g4, g5, g6, g7, g8, g9, g10];
if ([recognizers count < [_myArray count]) {
NSLog("Houston, we have a problem! Not enough gesture recognizers to go around.");
}
for (int i=0; i < [_myArray count]; i++)
{
myClassInstance.recognizer = recognizers[i]; // note the fancy "new" array access syntax!
}
If you're not allocating the gesture recognizers individually, then you can just create one each time though the loop:
for (MyClass *instance in _myArray) {
instance.recognizer = [[UIGestureRecognizer alloc] init...];
}

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

How to show values form custome object in iphone app

I have custom object and I have stored my values in Array. but I am bit stuck to show objects values form array. My code definition is here.
for (int i = 0; i < 20; i++)
{
Person *myPerson = [[Person alloc] init];
myPerson.name = #"Brian";
myPerson.age = [NSNumber numberWithInteger:23];
[myArray addObject:myPerson];
[myPerson release];
}
Now I want to show all 20 values which is stored in Array (name and age of person).
How will I show that values?
There are many different ways of showing the customers depending on what you want.
1. Print to the console
If you just want to print them out to the console, you can use:
for (int i = 0; i < 20; i++)
{
Person *thisPerson = [myArray objectAtIndex:i];
NSLog(#"%# has an age of %d", thisPerson.name, thisPerson.age);
}
additionally, you can use Fast Enumerators to neaten things up:
for (Person *thisPerson in myArray)
{
NSLog(#"%# has an age of %d", thisPerson.name, thisPerson.age);
}
2. Showing in a table view
You'll need a UITableView with an instance of UITableViewController that conforms to the UITableViewDataSource protocol.
This tutorial gives you an excellent walkthrough:
http://www.icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/
If neither of these solutions suits, please provide more information about what you're trying to achieve.

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

remove Annotation removes random amount of annotations at a time

I've got this code to erase an annotations (pins) in my mkmapview without erasing my blue dot (userLocation). The problem is that it erasing the pins I've added in seemingly random numbers. when it's called through an IBAction it removes the first 5 then click again it removes the next 3, then the next 2, then the last one.
When pressed I need it to remove that latest pin...etc. etc.
for (int i = 0;
i < [mapView.annotations count];
i++
)
{ if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotation class]])
{
[mapView removeAnnotation:[mapView.annotations objectAtIndex:i]];
}
}
The problem is that you are modifying the annotation collection while iterating over it. At every execution of the loop, the loop's termination condition [mapView.annotations count] changes its value. This will lead to unforeseen behavior. You should
either put all annotations you want to remove into an empty mutable array inside the loop an then call removeAnnotations: with this array as a parameter after you exit the loop,
or count down from the annotation with the highest index to 0.
Use this code
NSInteger *counter = [mapView.annotations count];
for (int i = 0; i < counter; i++ )
{
if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotation class]])
{
[mapView removeAnnotation:[mapView.annotations objectAtIndex:i]];
}
}

iPhone OS: Using an NSString variable to specify a property or ivar

I have a program that I want to either hide or show certain UIbuttons depending on certain variables and all the buttons are named incrementally like 'button1, button2, button3.'
So I want to iterate through the buttons but I don't know how to address the button in an assignment statement using an nsstring as a variable inside a dot notation assignment, such as:
for (int i = 1; i < weekday; i++) {
int buttonIncrement = 0;
NSString *tempString = [[NSString alloc] initWithFormat:
#"calbutton%i", buttonIncrement];
self.tempString.hidden = YES;
}
The "tempString" part of the assignment I want to tell it to insert "calbuttonx" where x is the button number.
Can you do this somehow? if so please explain.
Use an array!
If you can't use an array, you can reference to a property by string with Key-Value Coding (KVC):
UIButton* button = [self valueForKey:tempString];
button.hidden = YES;
You can also assign a tag to each button in IB and get the button associated with the tag using
- (UIView *)viewWithTag:(NSInteger)tag
as defined on class UIView, for example:
for( int k = 0; k < 5; ++k ) {
id subview = [self.view viewWithTag: k];
if( subview ) {
...
}
}