web-dev-qa-db-ja.com

iOS8で廃止されたU​​IActionSheet

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インデックスを長押しするとアクションシートがアクティブになります。

上記のコードを適切に実装するにはどうすればよいですか?

14
Denis Candido

同じために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)
52
Nilesh Jha