iPhone: Pop up a list of options


/ Published in: Objective C
Save to your folder(s)

Here's a quick snippet for popping up a list of options using the UIActionSheet in iPhone OS.

What's slightly confusing is that the index of the 'cancelButton' is 1, and 'destructiveButton' is 0, even though 'cancelButton' is listed first in the initWithTitle selector. This gets me each time!


Copy this code and paste it in your HTML
  1. // place this code in a UIViewController subclass
  2.  
  3. - (void)debugButtonPressed:(id)sender
  4. {
  5. UIActionSheet* action = [[UIActionSheet alloc]
  6. initWithTitle:@"Menu"
  7. delegate:self
  8. cancelButtonTitle:@"Continue"
  9. destructiveButtonTitle:@"Quit"
  10. otherButtonTitles:@"Debug",nil ];
  11. [action showInView:self.view];
  12. [action release];
  13. }
  14.  
  15. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  16. switch (buttonIndex) {
  17. case 0: // Quit
  18. NSLog(@"Quit");
  19. break;
  20. case 1: // Continue
  21. NSLog(@"Continue");
  22. break;
  23. case 2: // Debug
  24. NSLog(@"Debug");
  25. break;
  26. }
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.