UIActionSheet deprecated on iOS8 [duplicate] - ios

This question already has an answer here:
UIActionsheet in Ios8
(1 answer)
Closed 6 years ago.
I want to use UIActionSheet for iOS8 but it's deprecated and I don't know how to use the updated way to use this...
See the old code:
-(void)acoesDoController:(UIViewController *)controller{
self.controller = controller;
UIActionSheet *opcoes = [[UIActionSheet alloc]initWithTitle:self.contato.nome delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Delete" otherButtonTitles:#"other", nil];
[opcoes showInView:controller.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//switch case of the buttons
}
Just to make clear, in this example the action sheet is activated after a long press in an UITableView index.
How can I implement the code above in the properly way?

You can use UIAlertController for the same.
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:#"Action Sheet" message:#"alert controller" preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Cancel button tappped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// Distructive button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Other" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// OK button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
Note : Please Find the answer in Swift as well.
var actionSheet = UIAlertController(title: "Action Sheet", message: "alert controller", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
// Cancel button tappped.
self.dismiss(animated: true) {
}
}))
actionSheet.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { action in
// Distructive button tapped.
self.dismiss(animated: true) {
}
}))
actionSheet.addAction(UIAlertAction(title: "Other", style: .default, handler: { action in
// OK button tapped.
self.dismiss(animated: true) {
}
}))
// Present action sheet.
present(actionSheet, animated: true)
// Answer for SwiftUI
struct ContentView: View {
#State private var showingOptions = false
#State private var selection = "None"
var body: some View {
VStack {
Text(selection)
Button("Confirm paint color") {
showingOptions = true
}
.confirmationDialog("Select a color", isPresented: $showingOptions, titleVisibility: .visible) {
Button("Red") {
selection = "Red"
}
Button("Green") {
selection = "Green"
}
Button("Blue") {
selection = "Blue"
}
}
}
}
}

Related

IOS create UIAlertViewController programmatically

I'm working on a ViewController with code (no storyboard). I'm trying to add and AlertController
I have declare propery in .m
#property (nonatomic, strong) UIAlertController *alertController;
And init in loadview method
//alertviewController
_alertController = [[UIAlertController alloc]initWithNibName:nil bundle:nil];
And call the alertview in viewDidLoad:
_alertController = [UIAlertController alertControllerWithTitle:#"Error display content" message:#"Error connecting to server, no local database" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
LandingPageViewController *viewController = [[LandingPageViewController alloc] initWithNibName:nil bundle:nil];
// viewController.showNavBarBackButton = YES;
[[AppDelegate sharedAppDelegate].rootViewController cPushViewController:viewController];
}];
[_alertController addAction:ok];
[self presentViewController:_alertController animated:YES completion:nil];
I dont' know why the alert is not showing up. Some thing wrong with my code. How to set up and call alertViewController programmatically?
- (void)logoutButtonPressed
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:#"Logout"
message:#"Are You Sure Want to Logout!"
preferredStyle:UIAlertControllerStyleAlert];
//Add Buttons
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"Yes"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
[self clearAllData];
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle no, thanks button
}];
//Add your buttons to alert controller
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
}
In Xcode 9.4.1
Create alert function globally and use every where.
//Alert function
- (void) showAlertMsg:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message {
UIAlertController * alert = [UIAlertController alertControllerWithTitle : title
message : message
preferredStyle : UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{ }];
[alert addAction:ok];
dispatch_async(dispatch_get_main_queue(), ^{
[viewController presentViewController:alert animated:YES completion:nil];
});
}
Call like this in your required place.
Import that class and create instance
//Ex:
GlobalClassViewController *gcvc = [[GlobalClassViewController alloc]init];
//Call alert function with instance
[gcvc showAlertMsg:self title:#"" message:placeholderText];
How would I create a UIAlertView in Swift?
And in
Swift >=3:
let alertController = UIAlertController(title: "Some Error",
message: "Pleas confirm?",
preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self?.present(alertController, animated: true, completion: nil)
Create global Alert controller that is accessible in all view controllers using extension.
Create an extension of UIViewController with your function to display alert with required parameter arguments.
extension UIViewController {
func displayalert(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
alert.dismiss(animated: true, completion: nil)
})))
self.present(alert, animated: true, completion: nil)
}
}
Now call this function from your view controller:
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.displayalert(title: <String>, message: <String>)
}
}

How do I handle the actions of a UIAlertController?

How can I link UIAlertController alert action buttons with action handlers using objective-c? I'm using Xcode 7.1.
Here is my code:
- (IBAction)selectbtn:(UIButton *)sender {
UIAlertController *alert=[ UIAlertController alertControllerWithTitle:#"NEW" message:#"button pressed" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraaction=[UIAlertAction actionWithTitle:#"From camera" style:UIAlertActionStyleDefault handler:nil ];
[alert addAction:cameraaction];
UIAlertAction *libraryaction=[UIAlertAction actionWithTitle:#"From photo library" style:UIAlertActionStyleDefault handler:nil ];
[alert addAction:libraryaction];
UIAlertAction *cancelaction=[UIAlertAction actionWithTitle:#"cancel" style:UIAlertActionStyleDestructive handler:nil];
[alert addAction:cancelaction];
[self presentViewController:alert animated:YES
completion:nil];
}
Objective-C
UIAlertController works like this:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"text mssg" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Ok action example
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:#"Other" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Other action
}];
[alert addAction:okAction];
[alert addAction:otherAction];
[self presentViewController:alert animated:YES completion:nil];
I think you meant that.
Swift 3.0/4.0
let myalert = UIAlertController(title: "Titulo mensaje", message: "Mi mensaje.", preferredStyle: UIAlertControllerStyle.alert)
myalert.addAction(UIAlertAction(title: "Aceptar", style: .default) { (action:UIAlertAction!) in
print("Selected")
})
myalert.addAction(UIAlertAction(title: "Cancelar", style: .cancel) { (action:UIAlertAction!) in
print("Cancel")
})
self.present(myalert, animated: true)
Swift 5
let myalert = UIAlertController(title: "Titulo mensaje", message: "Mi mensaje.", preferredStyle: UIAlertController.Style.alert)
myalert.addAction(UIAlertAction(title: "Aceptar", style: .default) { (action:UIAlertAction!) in
print("Selected")
})
myalert.addAction(UIAlertAction(title: "Cancelar", style: .cancel) { (action:UIAlertAction!) in
print("Cancel")
})
self.present(myalert, animated: true)
You can add any code you want in the handler of the action method, sample code can be like this:
#interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (strong, nonatomic) UIAlertController *alertCtrl;
#property (strong, nonatomic) UIImagePickerController *imagePicker;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupAlertCtrl];
}
- (void) setupAlertCtrl
{
self.alertCtrl = [UIAlertController alertControllerWithTitle:#"Select Image"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
//Create an action
UIAlertAction *camera = [UIAlertAction actionWithTitle:#"From camera"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self handleCamera];
}];
UIAlertAction *imageGallery = [UIAlertAction actionWithTitle:#"From Photo Library"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self handleImageGallery];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[self dismissViewControllerAnimated:YES completion:nil];
}];
//Add action to alertCtrl
[self.alertCtrl addAction:camera];
[self.alertCtrl addAction:imageGallery];
[self.alertCtrl addAction:cancel];
}
- (IBAction)selectImagePressed:(UIButton *)sender
{
[self presentViewController:self.alertCtrl animated:YES completion:nil];
}
- (void)handleCamera
{
#if TARGET_IPHONE_SIMULATOR
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Error"
message:#"Camera is not available on simulator"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
#elif TARGET_OS_IPHONE
//Some code for iPhone
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePicker animated:YES completion:nil];
#endif
}
- (void)handleImageGallery
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.delegate = self;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
UIImage *img = [[UIImage alloc] initWithData:dataImage];
[self.imageView setImage:img];
[self.imagePicker dismissViewControllerAnimated:YES completion:nil];
}
Ref Link
UIAlertView Controller with multiple textFields.
#IBAction func showAlert(_ sender: UIButton) {
let alert = UIAlertController(title: "Registration", message: "Enter your name!", preferredStyle: .alert)
alert.addTextField { (textField1: UITextField) in
textField1.placeholder = "John"
}
alert.addTextField { (textField2: UITextField) in
textField2.placeholder = "Doe"
}
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction) in
if let textField1 = alert.textFields?.first {
if textField1.text == "" {
print("you have to enter something first")
}else {
print("Hello \(textField1.text!)")
}
}
if let textField2 = alert.textFields?.first {
if textField2.text == "" {
print("you have to enter something first")
}else {
print("Hello \(textField2.text!)")
}
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}

How to add dynamic buttons on UIAlertController?

I'm converting my code from using UIActionSheet to use UIAlertController.
The way I do it using UIActionSheet is like this:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Gender"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSDictionary *genderInfo in self.genderList) {
NSString *gender = [[genderInfo objectForKey:#"description"] capitalizedString];
[actionSheet addButtonWithTitle:gender];
}
[actionSheet addButtonWithTitle:#"Cancel"];
And just handle what button is pressed on the delegate method of action sheet.
While converting it to alert controller, I noticed that there is a handler on each of the alert action. I wonder how will I implement the alert controller to have dynamic buttons that I can handle the actions.
Here i mention code for load array into UIAlertController dynamically with image & text :Output Image here
NSArray *numbersArrayList = #[#"One", #"Two", #"Three", #"Four", #"Five", #"Six"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Numbers:"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
for (int j =0 ; j<numbersArrayList.count; j++){
NSString *titleString = numbersArrayList[j];
UIAlertAction *action = [UIAlertAction actionWithTitle:titleString style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
NSLog(#"Selected Value: %#",numbersArrayList[j]);
}];
[action setValue:[[UIImage imageNamed:#"USA16.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:#"image"];
[alertController addAction:action];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
Got it!
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Gender:"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
for (NSDictionary *genderInfo in self.genderList) {
NSString *gender = [[genderInfo objectForKey:#"description"] capitalizedString];
UIAlertAction *action = [UIAlertAction actionWithTitle:gender
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSString *title = action.title;
//you can check here on what button is pressed using title
}];
[alertController addAction:action];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
In Swift:
import Foundation
import UIKit
class AlertUtility {
static let CancelButtonIndex = -1;
class func showAlert(_ onController:UIViewController, title:String?,message:String? ) {
showAlert(onController, title: title, message: message, cancelButton: "OK", buttons: nil, actions: nil)
}
/**
- parameter title: title for the alert
- parameter message: message for alert
- parameter cancelButton: title for cancel button
- parameter buttons: array of string for title for other buttons
- parameter actions: action is the callback which return the action and index of the button which was pressed
*/
class func showAlert(_ onController:UIViewController?, title:String?,message:String? = nil ,cancelButton:String = "OK",buttons:[String]? = nil,actions:((_ alertAction:UIAlertAction,_ index:Int)->())? = nil) {
// make sure it would be run on main queue
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let action = UIAlertAction(title: cancelButton, style: UIAlertAction.Style.cancel) { (action) in
alertController.dismiss(animated: true, completion: nil)
actions?(action,CancelButtonIndex)
}
alertController.addAction(action)
if let _buttons = buttons {
for button in _buttons {
let action = UIAlertAction(title: button, style: .default) { (action) in
let index = _buttons.index(of: action.title!)
actions?(action,index!)
}
alertController.addAction(action)
}
}
if let onController = onController{
onController.present(alertController, animated: true, completion: nil)
}else{
// let appdel = UIApplication.shared.delegate as! AppDelegate
// appdel.window!.rootViewController?.present(alertController, animated: true, completion: nil)
}
}
}
Use in your VC:
//MARK:- IB-ACTION(S)
#IBAction func actionShowAlertButtonTapped(_ sender: Any) {
AlertUtility.showAlert(self, title: "Are you sure you want to start this order ?", cancelButton: "Cancel", buttons: ["OK", "Button 1", "Button 2", "Button 3", "Button 4","Button 5"], actions: {(action, index) -> () in
if index != AlertUtility.CancelButtonIndex {
//Cancel Button Action Here..
}else{
//Other Button Actions as per Button Index..
}
})
}

UIAlertController handle dismiss upon click outside (IPad)

Previous to iOS8 we used the UIActionSheet for showing alert and now we need to use the UIAlertController.
When we used the UIActionSheet we could easily handle situations where the user clicked outside the pop up (which means he want to cancel the operation) by comparing the clickedButtonAtIndex to the cancelButtonIndex - if the user indeed pressed outside the popup we got the cancel button index in this function.
How can we handle these situations with the new UIAlertController? I tried to use the "completion" block but it doesn't have any context. Is there an easy way to handle this? (other than "saving" the actions states in some general variable).
You can add an action with style:UIAlertActionStyleCancel and the handler for this action is called when the user taps outside the popup.
if ([UIAlertController class]) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Alert Title" message:#"A Message" preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(#"User clicked button called %# or tapped elsewhere",action.title);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(#"User clicked button called %#",action.title);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:#"Other" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(#"User clicked button called %#",action.title);
}]];
UIControl *aControl = (UIControl *) sender;
CGRect frameInView = [aControl convertRect:aControl.bounds toView:self.view];
alertController.popoverPresentationController.sourceRect = frameInView;
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:alertController animated:YES completion:nil];
}
The solution which works for UIAlertController with alert style. Just needed to add gesture recognizer to alertController superview.
[self presentViewController: alertController
animated: YES
completion:^{
alertController.view.superview.userInteractionEnabled = YES;
[alertController.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(alertControllerBackgroundTapped)]];
}];
- (void)alertControllerBackgroundTapped
{
[self dismissViewControllerAnimated: YES
completion: nil];
}
UITapGestureRecognizer didn't work for me so I've used this way:
func addDismissControl(_ toView: UIView) {
let dismissControl = UIControl()
dismissControl.addTarget(self, action: #selector(self.dismissAlertController), for: .touchDown)
dismissControl.frame = toView.superview?.frame ?? CGRect.zero
toView.superview?.insertSubview(dismissControl, belowSubview: toView)
}
func dismissAlertController() {
self.dismiss(animated: true, completion: nil)
}
func presentAlertController(title: String?, message: String?, preferredStyle: UIAlertControllerStyle, handler: ((UIAlertAction) -> Swift.Void)? = nil, completion: (() -> Swift.Void)? = nil) {
let alertController = UIAlertController(title: title, message: nil, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default) { (alertAction) -> Void in
handler?(alertAction)
})
self.present(alertController, animated: true, completion: {
self.addDismissControl(alertController.view)
completion?()
})
}
func someWhereInYourViewController() {
// ...
presentAlertController(title: "SomeTitle", message: "SomeMessage", preferredStyle: .actionSheet, handler: { (alertAction) -> Void in
//do some action
}, completion: {
//do something after presentation
})
// ...
}

UIPopover How do I make a popover with buttons like this?

I'm wondering how I can create a popover with buttons like this.
ANSWER:
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate: self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: #"Take Photo",
#"Choose Existing Photo", nil];
[actionSheet showFromRect: button.frame inView: button.superview animated: YES];
Somewhere else in your delegated object class...
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
// take photo...
}
else if (buttonIndex == 1) {
// choose existing photo...
}
}
This is a UIActionSheet. On the iPhone, it animates in from the bottom. On the iPad it appears in a popover.
Assuming you're doing this on the press of a button:
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate: self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: #"Take Photo",
#"Choose Existing Photo", nil];
[actionSheet showFromRect: button.frame inView: button.superview animated: YES];
In iOS8+, you should use the new UIAlertController class:
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
message: nil
preferredStyle: UIAlertControllerStyleActionSheet];
[alertController addAction: [UIAlertAction actionWithTitle: #"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Handle Take Photo here
}]];
[alertController addAction: [UIAlertAction actionWithTitle: #"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Handle Choose Existing Photo here
}]];
alertController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController * popover = alertController.popoverPresentationController;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
popover.sourceView = sender;
popover.sourceRect = sender.bounds;
[self presentViewController: alertController animated: YES completion: nil];
or in Swift
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
alertController.addAction(UIAlertAction(title: "Take Photo", style: .Default, handler: { alertAction in
// Handle Take Photo here
}))
alertController.addAction(UIAlertAction(title: "Choose Existing Photo", style: .Default, handler: { alertAction in
// Handle Choose Existing Photo
}))
alertController.modalPresentationStyle = .Popover
let popover = alertController.popoverPresentationController!
popover.permittedArrowDirections = .Up
popover.sourceView = sender
popover.sourceRect = sender.bounds
presentViewController(alertController, animated: true, completion: nil)
Similar to the other responses, but this is very easy to implement in comparison.
Make your class use the UIActionSheetDelegate.
Example:
#interface ExampleViewController : UIViewController <UIActionSheetDelegate>
Then add to your ExampleViewController.mm/m
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //Get the name of the current pressed button
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Remove"]) {
NSLog(#"Remove this actionSheet"); }
if ([buttonTitle isEqualToString:#"Button 1"]) {
NSLog(#"Button 1 pressed"); }
if ([buttonTitle isEqualToString:#"Button 2"]) {
NSLog(#"Button 2 pressed"); }
if ([buttonTitle isEqualToString:#"Button 3"]) {
NSLog(#"Button 3 pressed"); }
if ([buttonTitle isEqualToString:#"Cancel"]) {
NSLog(#"Cancel clicked (anywhere away from it)"); } }
Now in a button pressed event or where/when you want this to popup call the following:
- (IBAction)aButtonPressed:(id)sender {
NSString *actionSheetTitle = #"Action Sheet"; // Title
NSString *destroyTitle = #"Destroy"; // Button titles
NSString *button1 = #"Button 1";
NSString *button2 = #"Button 2";
NSString *button3 = #"Button 3";
NSString *cancelTitle = #"Cancel";
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:actionSheetTitle
                              delegate:self
                               cancelButtonTitle:cancelTitle
                             destructiveButtonTitle:destroyTitle
                              otherButtonTitles:button1, button2, button3, nil]; [actionSheet showInView:self.view];
}
And more information about this # :
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html

Resources