[英]how to use UIAlertController
I am trying to use this code in a Pacman game I got from some website but had to change UIAlertView
to UIAlertController
except the following code has two errors that I don't know how to fix (I am really new to programming and feel like this is a really newbie question - sorry!!)我正在尝试在从某个网站获得的 Pacman 游戏中使用此代码,但不得不将
UIAlertView
更改为UIAlertController
除非以下代码有两个我不知道如何修复的错误(我对编程非常UIAlertController
,感觉像这样是一个非常新手的问题 - 抱歉!!)
The first error is in line 4: No known class method for selector alertControllerWithTitle
第一个错误在第 4 行:No known class method for selector
alertControllerWithTitle
A second error is in the last line: no visible interface declares the selector show
第二个错误在最后一行:no visible interface声明选择器
show
- (void)collisionWithExit: (UIAlertController *)alert {
if (CGRectIntersectsRect(self.pacman.frame, self.exit.frame)) {
[self.motionManager stopAccelerometerUpdates];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Congratulations"
message:@"You've won the game!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil
preferredStyle:UIAlertControllerStyleAlert];
[alert show];
}
}
Please check the following code:请检查以下代码:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
Check below this code.检查下面这个代码。
for Objective-C:对于 Objective-C:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//button click event
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancel];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
for Swift 4.x:对于 Swift 4.x:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style {
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}
}))
self.present(alert, animated: true, completion: nil)
Swift 5: Simple Extension Swift 5:简单扩展
extension UIViewController {
func presentAlert(withTitle title: String, message : String, actions : [String: UIAlertAction.Style], completionHandler: ((UIAlertAction) -> ())? = nil) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
for action in actions {
let action = UIAlertAction(title: action.key, style: action.value) { action in
if completionHandler != nil {
completionHandler!(action)
}
}
alertController.addAction(action)
}
self.present(alertController, animated: true, completion: nil)
}
}
Usage:用法:
self.presentAlert(withTitle: "Network Error", message: "Please check your internet connection", actions: [
"Retry" : .default, "Cancel": .destructive] , completionHandler: {(action) in
if action.title == "Retry" {
print("tapped on Retry")
}else if action.title == "Cancel" {
print("tapped on Cancel")
}
})
OR或者
self.presentAlert(withTitle: "Mail services are not available", message: "Please Configure Mail On This Device", actions: ["OK" : .default] , completionHandler: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.