views:

1221

answers:

2

I am attempting to programmatically add buttons to my screen while looping through an array. The code I've posted below works just fine in the simulator (and executes without error on the phone), however, the button is not added to the screen on the phone. Any suggestions as to where I've gone wrong?

// loop through missions, dropping buttons
UIButton *button;
for(mission in activeMissions){

    // add a button for the mission
    buttonImage = [UIImage imageNamed:@"missionIconAttack.png"];
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0,0, buttonImage.size.width, buttonImage.size.height);
    button.center = CGPointMake(60, 298); // hard coded for simplicity
    button.tag = [missionButtons count];
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button addTarget:self action:@selector(SelectMission:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [missionButtons addObject:button];
    [button release];
    button = nil;
}
+1  A: 

UIButton's class method creates an autorelease object.

Not sure why it works in the simulator, but you don't need the release or the nil.

Your codes does put the created buttons in the same location in the view (right on top of each other), not sure if that's what you want.

Jordan
Nice catch. The autorelease isn't quite the problem though. It seems <code>buttonImage = [UIImage imageNamed:@"missionIconAttack.png"];</code> is returning a null pointer. Still looking into it...
Jason George
A: 

After some further debugging, it seems the iPhone is case sensitive when accessing the file system (or at least in this case), while the iPhone Simulator is not.

I removed some switch statements when I posted for simplicity. The offending culprit was @"missionIconPickup.png"; within an image assignment switch, which should have been @"missionIconPickUp.png";.

It would have been nice if the simulator had caught that. I'm a little embarrased to have posted with a typo having been the problem.

Jason George