【问题标题】:UIActionSheet deprecated on iOS8 [duplicate]iOS8 上不推荐使用 UIActionSheet [重复]
【发布时间】:2016-08-03 13:01:10
【问题描述】:

我想为 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 索引中长按后被激活。

我怎样才能以正确的方式实现上面的代码?

【问题讨论】:

  • 为了将来参考,如果您查看UIActionSheet 的文档,它会清楚地标记为自 iOS8 起已弃用,它甚至会告诉您替换类是什么。当您访问UIAlertController 的文档时,它甚至会为您提供如何使用它的示例。学习阅读文档是改进的重要第一步。
  • 感谢您的提示!我还在使用 Objective-c。

标签: ios objective-c ios8 deprecated uiactionsheet


【解决方案1】:

您也可以使用 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)

【讨论】:

  • Xcode 显示每个方法“ [self dismissViewControllerAnimated:YES completion:^{ }]; ”没有在@interfaces 中声明......我该如何解决它?......同样的事情发生在" [self presentViewController:actionSheet animated:YES completion:nil]; "
  • 你能分享你更新的代码吗?
  • 我已经解决了,谢谢。我试图在与 tableView 不同的类中实现这一点。我移动了代码,一切正常......但是......是否可以为此方法创建另一个类?如果你还需要我可以分享我的代码。
  • 删除 [self presentViewController:actionSheet animated:YES completion:nil]; UIAlertController 将在处理程序被调用后自动关闭
  • @nova,我删除了这一行并且没有显示操作表?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多