【问题标题】:Cancel Button in UIAlertController with UIAlertControllerStyle.ActionSheet使用 UIAlertControllerStyle.ActionSheet 取消 UIAlertController 中的按钮
【发布时间】:2014-11-16 10:37:42
【问题描述】:

我想在我的 UIAlert 中添加一个单独的取消按钮。

我知道如何使用 UIActionSheet 来做到这一点,但 UIAlert 也应该可以做到,对吧?

var sheet: UIActionSheet = UIActionSheet();
    let title: String = "...";
    sheet.title  = title;
    sheet.delegate = self;
    sheet.addButtonWithTitle("Cancel");
    sheet.addButtonWithTitle("...")
    sheet.cancelButtonIndex = 0;
    sheet.showInView(self.view);

这将有一个 ... 按钮和一个分开的取消按钮。

那么有谁知道如何使用

    var alert = UIAlertController(title: "...", message: "....", preferredStyle: UIAlertControllerStyle.ActionSheet)

?

我是 xcode 的新手,如果这个问题是愚蠢的,我很抱歉......

【问题讨论】:

    标签: ios swift xcode uialertcontroller uialertaction


    【解决方案1】:

    它真的很简单,但工作方式与他们过去的工作方式略有不同。现在您将“操作”添加到警报中。然后这些操作由设备上的按钮表示。

    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    

    上面是一个简单的取消按钮所需的代码 - 请记住,警报的解除是自动完成的,所以不要将它放在您的处理程序中。如果你想创建另一个按钮来做某事,请使用下面的代码:

    alert.addAction(UIAlertAction(title: "Button", style: UIAlertActionStyle.Default, handler: { action in
            println("This button now calls anything inside here!")
        }))
    

    希望我已理解您的问题,并回答了您的问题。我还要补充一点,在您添加了所有“动作”之后,您可以使用以下代码显示警报:

    self.presentViewController(alert, animated: true, completion: nil)
    

    希望这会有所帮助!

    【讨论】:

    • 虽然这可行,但它并没有回答他关于为取消按钮添加单独的问题的部分,就像 UIActionSheet 所做的那样。 UIAlertController 不会像 UIActionSheet 一样在取消按钮之前添加分隔符。
    • @PsychoDad 你说得对! - 当我第一次回答这个问题时,我一定误解了这个问题。我不知道在不编写自定义 UIAlert 的情况下创建分隔符的方法,所以恐怕无法修改这个问题。
    • 如果您添加的动作样式设置为 UIAlertActionStyle.Cancel,则会自动添加分隔符。
    • @MiroslavHrivik 不,即使样式设置为取消,也不添加分隔符。
    【解决方案2】:

    我想继续为特定问题提供特定答案。用户询问“取消”按钮的实现,而不是默认按钮。看看下面的答案!

    let alertController = UIAlertController(title: "Select one", message: "Hey! Press a button", preferredStyle: .actionSheet)
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    
    alertController.addAction(cancelAction)
    
    self.present(alertController, animated: true, completion: nil)
    

    【讨论】:

    • 这是正确的响应,它显示了如何添加分隔符,感谢style: .cancel
    【解决方案3】:

    这可能是你见过的最糟糕的编码答案,但我可以通过尝试满足你的要求:

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
    UILabel *alertLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, alertController.view.frame.size.width, 2)];
    alertLine.backgroundColor=[UIColor blackColor];
    [alertController.view.preferredFocusedView addSubview:alertLine];
    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:ok];
    [self.navigationController presentViewController:alertController animated:YES completion:nil];
    

    【讨论】:

    • 这里的问题是问题被标记为“swift”,而不是“objective-c”。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多