iOS8上不推荐使用UIActionSheet

Den*_*ido 13 objective-c deprecated uiactionsheet ios 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

}
Run Code Online (Sandbox Code Playgroud)

为了说清楚,在这个例子中,在UITableView索引中长按后激活了操作表.

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

Nil*_*Jha 48

您可以使用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];
Run Code Online (Sandbox Code Playgroud)

注意:请在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)
Run Code Online (Sandbox Code Playgroud)