iOS8 已弃用 UIActionSheet

UIActionSheet deprecated on iOS8

我想为 iOS8 使用 UIActionSheet,但它已被弃用,我不知道如何使用更新后的方式来使用它...

查看旧代码:

-(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

}

为了清楚起见,在此示例中,在 UITableView 索引中长按后激活操作 sheet。

如何以正确的方式实现上面的代码?

您可以使用 UIAlertController 来达到同样的目的。

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];

注意:请在Swift中找到答案。

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)

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