Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDee Greene
8,508 PointsUIActionSheet deprecated
I believe that UIActionSheet is now deprecated in current versions of iOS and xCode Ash Furrow
2 Answers
Danny Yassine
9,136 PointsHi there,
UIActionSheet and UIAlertView are deprecated from iOS 8 and above. If you want to use these type of views, you must use UIAlertController and set the style to either UIAlertControllerStyleAlertSheet or UIAlertControllerStyleAlert.
This is from the Apple Docs: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertController_class/
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
// CODE WHEN BUTTON WAS PRESSED
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
You must add UIAlertAction's (which are the buttons of those views). With UIAlertController, you dont need to use a delegate to determine which button was pressed, you put the code you want inside handler of those UIAlertAction, then add them to the UIAlertController.
Good luck!
Danny Y.
Dee Greene
8,508 PointsThis is my updated code for the video. However I'm not sure if this is the correct way to implement the "Cancel" button. Ash Furrow
- (void)promptForSource {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Image Source"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *camera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self promptForCamera];
}];
UIAlertAction *photoRoll = [UIAlertAction actionWithTitle:@"Photo Roll" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self promptForPhotoRoll];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
}];
[alert addAction:camera];
[alert addAction:photoRoll];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
Alexander Hickman
6,170 PointsI was going to post this exact code but I'm 5 months late :P There is a different style for cancel: UIAlertActionStyleCancel, this creates a separate cancel bubble button with darker text. I then just had the handler be nil since it automatically dismisses the view.