Search code examples
iosobjective-cios8deprecateduiactionsheet

UIActionSheet deprecated on iOS8


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?


Solution

  • 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"
                    }
                }
            }
        }
    }