如何在IOS 9的UIAlertController中添加按钮
如何在iOS 9中使用如何在IOS 9的UIAlertController中添加按钮,ios,objective-c,iphone,uialertcontroller,Ios,Objective C,Iphone,Uialertcontroller,如何在iOS 9中使用UIAlertView,以及如何在UIAlertController UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title" message:@"Message"preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* yesButton = [UIAlertAction
UIAlertView
,以及如何在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];
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 }
实际上,在各自的完成块中按下ok和cancel按钮之后,您需要编写代码
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
}
如果点击按钮后不需要执行任何其他操作,您可以将这些块保留为
nil
:
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Yes, please"
style:UIAlertActionStyleDefault
handler:nil];
你已经添加了两个按钮,我们在这里写的是,你需要处理你的动作,不管你需要什么,这就是全部,兄弟,你需要添加更多的按钮。那么如何处理这个动作呢???我不明白:(@Anbu.karthikin初始按下按钮放置断点并检查,将调用NSlog,此处不需要UIAlertcontroller上的任何代理,如果您使用alertview,将调用代理,我们将处理进一步的过程,但在UIAlertcontroller不需要委托方法中,UIAlertaction是处理e的委托操作非常好的按钮。@anupgupta——很高兴听到这个消息,兄弟,起初我也面临这个问题,一天一天,我学会了自己,就像你们正在做的那样….@Anbu.Karthik,我的UIAlertController是全局变量,因为我有进度条在上面并更新它,但问题是当点击UIAlertAction按钮时它崩溃了。谢谢兄弟@anupgupta
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Yes, please"
style:UIAlertActionStyleDefault
handler:nil];