代码之家  ›  专栏  ›  技术社区  ›  Sidney Liu

为什么我的警报不能执行阻塞,也不能解除?

  •  0
  • Sidney Liu  · 技术社区  · 7 年前

    我用 UIAlertController 并在其中添加一些操作。但当我按下按钮时,一切都没有改变。(包括取消按钮)所以我只能重新启动这个应用程序才能解决这个问题。

    let alert = UIAlertController(title: “my alert”, message: “some message...”, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: “button1”, style: .default, handler: { (_) in
         print(“button 1 up inside”)
    }))
    alert.addAction(UIAlertAction(title: “button2”, style: .default, handler: { (_) in
         print(“button 2 up inside”)
    }))
    alert.addAction(UIAlertAction(title: “cancel”, style: .cancel, handler: { (_) in
    
    }))
    self.present(alert, animated: true, completion: nil)
    

    Xcode 10已构建。

    1 回复  |  直到 7 年前
        1
  •  1
  •   CharlesBnf    7 年前

    我在使用Sprite工具包的应用程序中遇到了类似的问题。解决方案如下:

    let connectActionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    
    connectActionSheet.addAction(UIAlertAction(title: "Button1", style: .default, handler: { (action:UIAlertAction) in
    
           //Code if this button is pressed
    }))
    
    connectActionSheet.addAction(UIAlertAction(title: "Button2", style: .default, handler: { (action:UIAlertAction) in
    
           //Code if button2 is pressed
    }))
    
    connectActionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    
    let vc = self.view?.window?.rootViewController
    if vc?.presentedViewController == nil {
           vc?.present(connectActionSheet, animated: true, completion: nil)
    }
    

    最后四行是它成功的原因。我希望这对你有帮助。

        2
  •  -1
  •   Sidney Liu    6 年前

    谢谢!我的视图控制器无法关闭此警报,因为这些代码

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    }
    
    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    
    }