How to manipulate multiple objects with similar names

Discussion in 'iPhone/iPad Programming' started by happybob, Apr 22, 2009.

  1. macrumors newbie

    Joined:
    Apr 18, 2009
    #1
    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
     
  2. macrumors regular

    Joined:
    Apr 9, 2009
    #2
    well, you could add all the objects to an array and then do something like:
    Code:
    for (UIButton *button in myArray) {
      button.transform = ...;
    }
    
    this would go through all "UIButton" objects in the array myArray
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    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.
     
  4. thread starter macrumors newbie

    Joined:
    Apr 18, 2009
    #4
    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.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.hidden = YES;
     
  5. macrumors regular

    Joined:
    Apr 9, 2009
    #5


    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.
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6

    posmove is an NSArray, right? So, what you'll want to do is:

    Code:
    [posmove objectAtIndex:i].hidden = YES;
    or
    Code:
    [[posmove objectAtIndex:i] setHidden:YES];
    P.S. Please use the CODE tags to include your code. Makes it easier for us all to read.
     
  7. thread starter macrumors newbie

    Joined:
    Apr 18, 2009
    #7
    Sorry but when I try those and then launch the simulator locks up. What I have done:

    Code:
    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:

    Code:
    NSArray *posmove = [[NSArray alloc] arrayWithObjects: pos1, pos2, pos3, pos4, etc, nil];
    
    Sorry I'm not getting this
     
  8. macrumors regular

    Joined:
    Apr 9, 2009
    #8
    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!
     
  9. thread starter macrumors newbie

    Joined:
    Apr 18, 2009
    #9
    Thanks. Everyone my problem with the errors was that I stupidly forgot that 0 is a number in the array.
     
  10. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    What class is pos1, pos2, etc.?

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

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

Share This Page