15

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?

denisb411
  • 581
  • 1
  • 7
  • 25
  • 1
    For future reference, if you look at the documentation for [UIActionSheet](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActionSheet_Class/) it's clearly marked as being deprecated as of iOS8, and it even tells you what the replacement class is. And when you go to the documentation of [UIAlertController](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html#//apple_ref/occ/cl/UIAlertController) it even gives you an example of how to use it. Learning to read the documentation is a vital first step to improvement. – Abizern Aug 03 '16 at 14:29
  • Thanks for the hint! I'm still beginnig with objective-c. – denisb411 Aug 03 '16 at 16:37

1 Answers1

60

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"
                }
            }
        }
    }
}
Nilesh Jha
  • 1,626
  • 19
  • 35
  • The Xcode is showing that every method " [self dismissViewControllerAnimated:YES completion:^{ }]; " is not declarated in @interfaces... How can I fix it?... Same happening for " [self presentViewController:actionSheet animated:YES completion:nil]; " – denisb411 Aug 03 '16 at 18:28
  • Can You share Your updated Code? – Nilesh Jha Aug 03 '16 at 19:03
  • I already solved it, thanks. I was trying to implement this in a class different than the tableView. I moved the code and everything worked properly... but... Is it possible to created another class for this method? If you still need I can share my code. – denisb411 Aug 03 '16 at 19:26
  • remove [self presentViewController:actionSheet animated:YES completion:nil]; UIAlertController will dissmis automatically after handler get called – Hamed Nova Dec 28 '16 at 12:35
  • @nova,I removed this line and no actionsheet show? – kemdo Jan 02 '17 at 05:59
  • thank you removing that line was all i meant. :) – Hamed Nova Jan 03 '17 at 12:30
  • @kemdo i mean no need to dismiss manually .it will do it after executing completion block – Hamed Nova Jan 23 '17 at 09:33