如何在 IOS 9 中的 UIAlertController 中添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35152650/
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
how to add button in UIAlertController In IOS 9
提问by Anup Gupta
how we use UIAlertView
in iOS 9 and how to add button in UIAlertController
我们如何UIAlertView
在 iOS 9 中使用以及如何在其中添加按钮UIAlertController
UIAlertController * alert=[UIAlertController
alertControllerWithTitle:@"Title" message:@"Message"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Yes, please"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
**//What we write here????????**
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"No, thanks"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
**//What we write here????????**
}];
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
回答by Anbu.Karthik
UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title"
message:@"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"Yes, please"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
/** What we write here???????? **/
NSLog(@"you pressed Yes, please button");
// call method whatever u need
}];
UIAlertAction* noButton = [UIAlertAction actionWithTitle:@"No, thanks"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
/** What we write here???????? **/
NSLog(@"you pressed No, thanks button");
// call method whatever u need
}];
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
swift
迅速
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
let yesButton = UIAlertAction(title: "Yes, please", style: .default, handler: {(_ action: UIAlertAction) -> Void in
/** What we write here???????? **/
print("you pressed Yes, please button")
// call method whatever u need
})
let noButton = UIAlertAction(title: "No, thanks", style: .default, handler: {(_ action: UIAlertAction) -> Void in
/** What we write here???????? **/
print("you pressed No, thanks button")
// call method whatever u need
})
alert.addAction(yesButton)
alert.addAction(noButton)
present(alert, animated: true) { _ in }
回答by Shehzad Ali
You actually need to write code after ok and cancel button pressed in their respective completion blocks.
您实际上需要在它们各自的完成块中按下确定和取消按钮后编写代码。
UIAlertController * alert=[UIAlertController
alertControllerWithTitle:@"Title" message:@"Message"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Yes, please"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[self okButtonPressed];
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"No, thanks"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[self cancelButtonPressed];
}];
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
- (void)cancelButtonPressed{
// write your implementation for cancel button here.
}
- (void)okButtonPressed{
//write your implementation for ok button here
}
回答by Yulia
You can just leave these blocks nil
if you're not needed any additional actions after the button tapping:
nil
如果您在点击按钮后不需要任何其他操作,您可以离开这些块:
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Yes, please"
style:UIAlertActionStyleDefault
handler:nil];