UIAlertController时出错

问与答 · aedanan · 发布于 1 年前 · 955 次阅读

代码如下

import UIKit

class ViewController: UIViewController,UIAlertViewDelegate,UIActionSheetDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //创建样式是Alert的UIAlertController
    let alertController = UIAlertController(title: "提示", message: "确定退出登录?", preferredStyle: UIAlertControllerStyle.Alert)


    //创建取消按钮
    let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler:nil)

    //创建确定按钮
    let otherAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: nil)


    //添加按钮
    alertController.addAction(cancelAction)
    alertController.addAction(otherAction)

    //弹出
    self.presentViewController(alertController, animated: true, completion: nil)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

==========================================================错误代码 My_AlertView[704:14688] Warning: Attempt to present on <_2_My_AlertView.ViewController: 0x7fa13361df10> whose view is not in the window hierarchy!

如何解决?

共收到 1 条回复
tmachc · #1 · 1 年前

alertController没问题, 错误在于 self.presentViewController(alertController, animated: true, completion: nil), self并没有presentViewController的能力。

回帖