0

I have an array of objects _schedule.games I want to display the Game property opponent in each game as I loop through the schedule.

    int x = 0;
    for (int i = 0; i < [_schedule.games count]; i++)
    {
        Game *game = [_schedule.games objectAtIndex:i];
        game.opponent = ((Game *) [_schedule.games objectAtIndex:i]).opponent;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];

        [button setTitle:[NSString stringWithFormat:@"%@", game.opponent] forState:UIControlStateNormal];

        [_gameScrollList addSubview:button];

        x += button.frame.size.width;

    }
4
  • Why are you adding all buttons at same position. You are not using x in setting frame. So all buttons are added at same location. Commented Aug 19, 2013 at 5:28
  • Print game.opponent in console and what is the use of rewriting this line. game.opponent = ((Game *) [_schedule.games objectAtIndex:i]).opponent; if you can directly access game .opponent Commented Aug 19, 2013 at 5:29
  • @NishantTyagi I forgot to change "x" back, it prints (null) but gives me two buttons *The array has 2 objects Commented Aug 19, 2013 at 5:30
  • Ya button title will not display anything if it is null. and loop iterates two times thats why two buttons are there. So first print your schedule.games array and check are you getting opponents there ? Commented Aug 19, 2013 at 5:33

1 Answer 1

1

1.

    Game *game = [_schedule.games objectAtIndex:i];

gives you the game instance inside the array,So no need to assign the property again as

game.opponent = ((Game *) [_schedule.games objectAtIndex:i]).opponent;

game.opponent has the value that is in the array object property and so you can call it directly as game.opponent .

2.

[NSString stringWithFormat:@"%@", game.opponent] says game.opponent is a string so no need to typecast it again as a NSString

So the method will be as

int x = 0;
for (int i = 0; i < [_schedule.games count]; i++)
{
    Game *game = (Game *)[_schedule.games objectAtIndex:i];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];
    [button setTitle:game.opponent forState:UIControlStateNormal];
    [_gameScrollList addSubview:button];
    x += button.frame.size.width;
}
Sign up to request clarification or add additional context in comments.

3 Comments

What is the point of incrementing x in the loop ? Its not used anywhere in the above code. The CGRect(20,0,100,100) in your code should be replaced with CGRect(x+20,0,100,100); to bring it in effect.
I answered the question alone.Your point is right by the way,that increment should be used with x or y in setting the proper frame else buttons overlap
@CodenameLambda1 I use x, just made a change and forgot to change it back when I posted. It turns out that I wasn't init my game correctly, thus game.opponent was null. However, I'm going to accept your answer as it's a little cleaner than my method without the extra code that I didn't need. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.