如何使用UIAlertController

31

我试图在一个我从某个网站获取的Pacman游戏中使用这段代码,但是不得不将UIAlertView更改为UIAlertController。然而,下面的代码存在两个错误,我不知道如何修复(我是编程新手,感觉这是一个非常初级的问题 - 对不起!!)

第一个错误出现在第4行:alertControllerWithTitle没有已知的类方法选择器。

第二个错误出现在最后一行:没有可见的接口声明 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];
    }
}

AlertView在iOS 9中已经被弃用,因此您必须使用带有更多选项的UIAlertController - vaibhav
3个回答

67
请检查以下代码:
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];

54

请在下方查看此代码。

针对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];

适用于 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)

2
今日免费次数已满, 请开通会员/明日再来
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)
    }
}

用法:

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")
        }
})

self.presentAlert(withTitle: "Mail services are not available", message: "Please Configure Mail On This Device", actions: ["OK" : .default] , completionHandler: nil)

enter image description here


the best. Thank you - Eduard Streltsov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接