当UIAlertController:在Swift中调用0x7ff211650140时发出警告

时间:2015-11-14 06:21:29

标签: ios swift uialertcontroller

有时在致电UIAlert

时会收到此警告
  

尝试加载视图控制器的视图   不允许取消分配,可能导致未定义的行为   ()

code used:

    func alert (dictKey: String){
        print(dictKey)
        let alertController = UIAlertController(title: nil, message: promptsArr[dictKey], preferredStyle: .Alert )
        let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in

        }
        alertController.addAction(OKAction)
        if self.presentedViewController == nil {
            delay(1.0){
                self.presentViewController(alertController, animated: true, completion: nil)
            }
        } else {
            self.presentedViewController!
        }

1 个答案:

答案 0 :(得分:2)

  1. 删除delay()

  2. 将逻辑放在self.presentedViewController中: (如果存在,则无需创建警报)

    func alert (dictKey: String){
        if self.presentedViewController == nil {
            let alertController = UIAlertController(
                title: nil,
                message: promptsArr[dictKey],
                preferredStyle: .Alert )
            let OKAction = UIAlertAction(title: "OK", style: .Default) {
                (action) in
            }
            alertController.addAction(OKAction)
    
            self.presentViewController(
                alertController,
                animated: true,
                completion: nil)
        }
    }