Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Created February 22, 2013 11:08
Show Gist options
  • Select an option

  • Save lamprosg/5012688 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/5012688 to your computer and use it in GitHub Desktop.
(iOS) ActionSheet and UIALert
//You need to add the <UIActionSheetDelegate> protocol in your .h file interface
//@interface BIDViewController : UIViewController <UIActionSheetDelegate>
//Action when a button is pressed
- (IBAction)buttonPressed:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Are you sure?"
delegate:self
cancelButtonTitle:@"No Way!"
destructiveButtonTitle:@"Yes, I'm Sure!"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
}
//In delegate: we put "self" so in this controller the below method will be triggered.
//It's an inherited method which we'll override
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
//Get what the user has chosen
if (buttonIndex != [actionSheet cancelButtonIndex]) //Not the cancel button
{
NSString *msg = nil;
if (self.nameField.text.length > 0)
msg = [NSString stringWithFormat:"You can breathe easy, %@, everything went OK.",
self.nameField.text];
else
msg = @"You cancelled.";
}
}
//An action that pops up an alert
- (IBAction)yellowButtonPressed
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Yellow View Button Pressed"
message:@"You pressed the button on the yellow view"
delegate:nil
cancelButtonTitle:@"Yep, I did."
otherButtonTitles:nil];
[alert show];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment