Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,706
6,289
I'm trying to make my own Game Center interface, because I find the pool table style of the default one clashes with the style of the rest of my app.

When a player wants to start a game with a friend, my interface pops up an alert view, shows a spinner while it fetches their friends list, and then adds a button to the alert view for each friend.

Here's the code:
Code:
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Play with Someone Online"
                                                    message:@"Loading Game Center Friends...\n\n\n"
                                                   delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Play with Anyone", nil];
    [alert show];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.frame = CGRectMake(123.0, 70.0, 37.0, 37.0);
    [spinner startAnimating];
    [alert addSubview:spinner];
    [alert release];
    
    [[GKLocalPlayer localPlayer] loadFriendsWithCompletionHandler:^(NSArray *friends, NSError *error)
     {
         [spinner removeFromSuperview];
         if ([friends count] <= 0 || friends == nil)
         {
             [alert setMessage:@"You have no friends on Game Center, how sad."];
             [alert addButtonWithTitle:@"Find Friends by Email"];
         }
         
         else
         {
             [alert setMessage:@""];
             [GKPlayer loadPlayersForIdentifiers:friends withCompletionHandler:^(NSArray *players, NSError *error)
              {
                  [players enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop)
                   {
                       GKPlayer *player = (GKPlayer *)obj;
                       [alert addButtonWithTitle:player.alias];
                   }];
              }];
             [alert addButtonWithTitle:@"Find More Friends by Email"];
         }
     }];
}

The spinner shows up and hides fine, but the alert view doesn't add the buttons properly. Rather than lay them out nicely, it just throws the buttons with black text and no background in the top left corner.

Any suggestions on why it's not working or what I could do different?
 

Attachments

  • Screenshot 2011.08.21 13.09.54.png
    Screenshot 2011.08.21 13.09.54.png
    97.5 KB · Views: 186
I see you're releasing the alert and then calling it later in your code. Are you getting any error messages?

No, and when I have it log the retain count at the end it says it's 6.

I guess I could try not releasing it until the handlers are done with it.
 
Another thing I noticed. You're just calling addButtonWithTitle. I've never tried to add a button to an alertView, but don't you need to give coordinates for the button to be drawn? Sorry, I would look it up myself, but I don't have access to my computer right now.
 
Taking out the release didn't change anything. I'm fairly certain the UIAlertView is supposed to automatically position buttons.

Edit: Looking at some example code of using addButtonWithTitle:, it seems it's always called before show, because the automatic positioning and stuff is done in the show method.
 
Last edited:
That's exactly what I've been finding. So maybe in your if/else statements you can create 2 different UIAlertViews. One for the if statement and one for the else statement. Would that work?
 
Alright, I was able to make the alert view display properly by moving [alert show]; from the very start to after all the buttons had been added. I don't really like the solution, though, as it means the user gets no feedback from hitting a button until after its managed to load their entire friends list from game center... on the simulator where it has ideal data speeds and a friends list with only one player, it's fine, but in a real world setting a user could be on a device with spotty edge coverage and have a much longer friends list, which could result in a considerable delay in which the user isn't sure if it registered their touch at all... I'll have to rethink this interface a little bit...

Edit: :-/ The built in behavior for having too many buttons to fit in a UIAlertView is absolutely horrid. It just draws the buttons that don't fit right above the alert view and right off the screen. I can't believe Apple has that as standard behavior. I'll have to write my own custom subclass that displays a list of options in a UIAlertView... yes, I realize a modal list view would do the trick, but I don't really like the appearance of them... I feel like modal list views are clunky and force the user to slow down. Visually they just say "I'm slow and an inefficient use of screen space!" to me anytime I see them.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.