使用Swift 4显示错误警报

时间:2018-03-17 11:41:11

标签: swift error-handling

以下代码在调用.present时为我提供了一个sigabort:

    func alert(message: String, title: String = "") {

        let alertController =  UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
            switch action.style {
                case .default:
                    print("default")
                case .cancel:
                    print("cancel")
                case .destructive:
                    print("destructive")
            }}))
        alertController.present(alertController, animated: true, completion: nil)
    }

帮助!我没有得到这个!

1 个答案:

答案 0 :(得分:0)

您正在调用alertController而不是视图。更改它以引用父UIViewController的存在(...)。

func present(_ viewControllerToPresent: UIViewController, 
    animated flag: Bool, 
  completion: (() -> Void)? = nil)

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621380-present