ios 使用 UIAlertControllerStyle.ActionSheet 取消 UIAlertController 中的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26956016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cancel Button in UIAlertController with UIAlertControllerStyle.ActionSheet
提问by Hyman Chorley
I want to add a separated cancel button to my UIAlert.
我想在我的 UIAlert 中添加一个单独的取消按钮。
I know how to do it with UIActionSheet but it should also be possible with UIAlert, right?
我知道如何使用 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);
This will have a ... button and a cancel button which is separated.
这将有一个 ... 按钮和一个分开的取消按钮。
So does anyone know how to do this with
那么有谁知道如何做到这一点
var alert = UIAlertController(title: "...", message: "....", preferredStyle: UIAlertControllerStyle.ActionSheet)
?
?
I'm new to xcode and swift so sorry if this question is dumb or anything...
我是 xcode 和 swift 的新手,如果这个问题很愚蠢或任何其他问题,我很抱歉......
回答by Hyman Chorley
Its really simple, but works a bit differently to how they used to work. Now you add "actions" to your alerts. These actions are then represented by buttons on the device.
它真的很简单,但与以前的工作方式略有不同。现在您将“操作”添加到您的警报中。然后这些操作由设备上的按钮表示。
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
Above is the code needed for a simple cancel button - bear in mind that dismissal of the alert is done automatically so don't put that in your handler. Should you then want to create another button which does something, use the code below:
上面是一个简单的取消按钮所需的代码 - 请记住,警报的解除是自动完成的,所以不要把它放在你的处理程序中。如果您想创建另一个按钮来执行某些操作,请使用以下代码:
alert.addAction(UIAlertAction(title: "Button", style: UIAlertActionStyle.Default, handler: { action in
println("This button now calls anything inside here!")
}))
Hopefully I have understood your question and this answers what you were asking. I will also add that after you have added all of the "actions", you present the alert using the code below:
希望我已经理解了你的问题,这回答了你的问题。我还要补充一点,在您添加了所有“操作”之后,您将使用以下代码显示警报:
self.presentViewController(alert, animated: true, completion: nil)
Hope this helps!
希望这可以帮助!
回答by Kenny Batista
I wanted to go ahead and provide a specific answer for a specific question. The user asked about the implementation of a "cancel" button, not a default button. Check out the answer below!
我想继续为特定问题提供特定答案。用户询问“取消”按钮的实现,而不是默认按钮。看看下面的答案!
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)
回答by Pradeep Mittal
This might be the worst coded answer you would have seen, but I was able to meet your requirement by trying this:
这可能是您所见过的最糟糕的编码答案,但我可以通过尝试满足您的要求:
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];