Monday, March 25, 2013

How to display multiple buttons/options on a popup message


  1. Open the ViewController.h file and paste the following code to declare the event handler within the @interface section:
  2. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;

  3. Open the ViewController.m file and paste the following code to trigger the dialog:

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title Here" message:@"This is the message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert setTag:12];
        [alert addButtonWithTitle:@"Option 1"];
        [alert addButtonWithTitle:@"Option 2"];
        [alert show];
  4. Paste the following code to implement the event handler within the @implementation section:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
    {
        if ([alertView tag] == 12)
        {
            if (buttonIndex == 0)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Response" message:@"You chose the default button (which is Cancel in this case)" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
            else if (buttonIndex == 1)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Response" message:@"You selected Option 1" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
            else if (buttonIndex == 2)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Response" message:@"You selected Option 2" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
        }
    }
  5. Done. Lets run it:



    When you click the Option 1 button, you get:

No comments:

Post a Comment