NSMutableArray of UIButton objects

Discussion in 'iOS Programming' started by XcodeNewb, Jul 29, 2009.

  1. XcodeNewb macrumors member

    Feb 6, 2009
    I have an NSMutableArray that I have filled with UIButton objects. The problem I am having is that on a certain user action I want to hide all of the buttons that I have added to that array.

    I tried something like:

    [[myDateArray objectAtIndex:i] setHidden];
    but this did not work. It is like I lose that fact that the objects in the array are of UIButton type.

    Any ideas how I can retrieve the type of the object to use it's properties?

  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    It's true that when you get an object back from an array it has a type of id but you can certainly send it messages. These should work, for instance:

    UIButton*  b = [myDateArray objectAtIndex:i];
    b.hidden = YES;
    [[myDateArray objectAtIndex:i] setHidden:YES];
  3. XcodeNewb thread starter macrumors member

    Feb 6, 2009
    Thanks Phoney. I swear I tried both of those and they didn't work. Works fine now though. The little things we waste our time on are amazing!
  4. moral-hazard macrumors regular

    Jul 27, 2009
    Palo Alto, CA
    Obj-C does not know that all of the objects in the array are UIButtons - cast it. Phoney's code does the same thing. You basically have to "force" obj-C to treat it as a UIButton, which works out fine (assuming that it is).

    ((UIButton* )[[myDateArray objectAtIndex:i]).hidden = YES;

Share This Page