Tuesday, April 19, 2011

Adding Multiple Buttons to an Alertview and specifying their actions

Step 1. Open up our alertview project.. if you haven’t done that yet please watch the tutorial on that and then comeback

Alertview Tutorial:CLICK HERE

Step 2. Navigate to the (***ViewController.m)- the stars just represent the name

Step 3. Navigate to the alertview action (where the code is) it should look something like this

-(IBAction)pushAlert {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”This is a alert view” message:@”Watever” delegate:nil cancelButtonTitle:@”Cancel” otherButtonTitles:nil];
[alert show];
[alert release];

}


Step 4. Notice the line that says…. otherButtonTitles:nil]; this statement is saying there is no more buttons, we want to add more buttons though

Step 5. Instead of ….otherButtonTitles:nil]; replace that with this: otherButtonTitles:@”button 2″, @”button 3″, nil];

The statment should now look something like this

-(IBAction)pushAlert {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”This is an alert view” message:@”Whatever” delegate:nil cancelButtonTitle:@”Cancel” otherButtonTitles:@”button 2″, @”button 3″, nil];

}

Step 6. Add the void statement that will specify the actions

Build and Go the project and make sure the 3 buttons appear when you hit the button

Step 7. If the 3 buttons show up then add this statement into the project (press your space bar 3-4 times) and paste this code

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

if (actionSheet == actionSheet) {
if (buttonIndex == 1) {

type in an action here
}

if (buttonIndex == 2) {

type in an action here
}

if (buttonIndex == 3) {

type in an action here

}

}
}

No comments:

Post a Comment