Created
February 22, 2013 11:08
-
-
Save lamprosg/5012688 to your computer and use it in GitHub Desktop.
(iOS) ActionSheet and UIALert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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."; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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