PDA

View Full Version : How to manipulate multiple objects with similar names




happybob
Apr 22, 2009, 12:14 PM
Hi All, I'm trying to transform shift a series of buttons at the same time named: pos1, pos2, etc.

I can do the transformation easily:

pos1.transform = CGAffineTransform....

Is there a way to create an array or some other structure so that I only need a single instance of the transform:

allbutton.transform = CGAffineTransform....

Thanks



BlackWolf
Apr 22, 2009, 12:22 PM
Hi All, I'm trying to transform shift a series of buttons at the same time named: pos1, pos2, etc.

I can do the transformation easily:

pos1.transform = CGAffineTransform....

Is there a way to create an array or some other structure so that I only need a single instance of the transform:

allbutton.transform = CGAffineTransform....

Thanks

well, you could add all the objects to an array and then do something like:

for (UIButton *button in myArray) {
button.transform = ...;
}

this would go through all "UIButton" objects in the array myArray

dejo
Apr 22, 2009, 12:35 PM
Hi All, I'm trying to transform shift a series of buttons at the same time named: pos1, pos2, etc
Are you just moving the buttons (i.e. changing their origins) within the view? If so, you don't really need to use CGAffineTransform. You can just adjust their frames.

happybob
Apr 22, 2009, 12:48 PM
Thanks again BlackWolf. The only problem, which I forgot to mention, is that I need to be able to change which objects move. This is what I have:

int divide_int = 6 //All pos#'s under this are moved

NSArray *posmove = [[NSArray alloc] arrayWithObjects: pos1, pos2, pos3, etc, nil];

for ( i = divide_int, i <= number_of_objects_in_array, i++ ) {
posmove[i].transform = CGAffineTransform
}

Anyway to make this work?

Dejo: I'm also hiding the buttons that don't move which I can place into a similar setup once I get this working. Thanks, for the idea though.

What I also want to do is, but sadly it doesn't work.

posmove[i].hidden = YES;

BlackWolf
Apr 22, 2009, 01:07 PM
Thanks again BlackWolf. The only problem, which I forgot to mention, is that I need to be able to change which objects move. This is what I have:

int divide_int = 6 //All pos#'s under this are moved

NSArray *posmove = [[NSArray alloc] arrayWithObjects: pos1, pos2, pos3, etc, nil];

for ( i = divide_int, i <= number_of_objects_in_array, i++ ) {
posmove[i].transform = CGAffineTransform
}

Anyway to make this work?

Dejo: I'm also hiding the buttons that don't move which I can place into a similar setup once I get this working. Thanks, for the idea though.

actually, there are several ways.
you could do pretty much what you posted. look at NSArray's documentation and you will see that it has a "count" property which gives you the total number of elements in the array. and you can access a specific element via NSArray's objectAtIndex: method.

second posibility would be to take the code I posted, and simply add an index that you set to 0 before the loop and increase it inside the loop. inside the loop you check if the index is bigger than divide_int and if so, perform your transformation.

but in your case the first solution would probably be better.

dejo
Apr 22, 2009, 01:25 PM
What I also want to do is, but sadly it doesn't work.

posmove[i].hidden = YES;
posmove is an NSArray, right? So, what you'll want to do is:

[posmove objectAtIndex:i].hidden = YES;
or
[[posmove objectAtIndex:i] setHidden:YES];

P.S. Please use the CODE tags to include your code. Makes it easier for us all to read.

happybob
Apr 22, 2009, 01:30 PM
Sorry but when I try those and then launch the simulator locks up. What I have done:


for ( i = divide_int, i <= 8, i++ ) {
((UIButton *)[posmove objectAtIndex:i]).transform = CGAffineTransform...;
[posmove objectAtIndex:i].hidden = NO;
}


Also when I try exactly what you had dejo, it comes up with an error: "request for member 'hidden' in something not a structure or union"

The Array is written like this:


NSArray *posmove = [[NSArray alloc] arrayWithObjects: pos1, pos2, pos3, pos4, etc, nil];


Sorry I'm not getting this

BlackWolf
Apr 22, 2009, 01:49 PM
yeah, same thing happened to me when it comes to hidden. you should use setHidden:something
worked for me.

also, I think [something].somethingElse does not work. use [[something] setSomethingElse:value]

also, did you properly initialize your i variable as ain int?

AND you should look at the syntax of a for loop. it uses ; as seperators, not ,

seems to me like you should really go over the basics again!

happybob
Apr 22, 2009, 04:26 PM
Thanks. Everyone my problem with the errors was that I stupidly forgot that 0 is a number in the array.

dejo
Apr 22, 2009, 06:43 PM
Also when I try exactly what you had dejo, it comes up with an error: "request for member 'hidden' in something not a structure or union"

The Array is written like this:


NSArray *posmove = [[NSArray alloc] arrayWithObjects: pos1, pos2, pos3, pos4, etc, nil];


Sorry I'm not getting this
What class is pos1, pos2, etc.?

Also, you either wanna:
NSArray *posmove = [[NSArray alloc] initWithObjects: pos1, pos2, pos3, pos4, etc, nil];
or
NSArray *posmove = [NSArray arrayWithObjects: pos1, pos2, pos3, pos4, etc, nil];

BlackWolf
Apr 23, 2009, 12:29 AM
What class is pos1, pos2, etc.?

Also, you either wanna:
NSArray *posmove = [[NSArray alloc] initWithObjects: pos1, pos2, pos3, pos4, etc, nil];
or
NSArray *posmove = [NSArray arrayWithObjects: pos1, pos2, pos3, pos4, etc, nil];

lol, I didn't even notice that :D
@OP: make sure you know what convenience constructors (like arrayWithObjects:) do, that's really important!