繁体   English   中英

如何以编程方式解除UIAlertController没有任何按钮?

[英]How to programmatically dismiss UIAlertController without any buttons?

我正在展示一个没有任何按钮的UIAlertViewController,因为它只是告知用户上传正在进行中。 该应用程序应该将一些文件上传到Amazon S3(某些事情发生在并行线程上),我担心当我想解雇时,对警报视图控制器的引用会丢失。

什么可能是错的? 我甚至不知道如何调试这个,因为Debug区域没有错误?

我有一个类级属性: var uploadInProgressAlert = UIAlertController()

我使用此代码显示没有按钮的警报(它工作):

self.uploadInProgressAlert = UIAlertController(title: "Uploading", message: "Please wait.", preferredStyle: .Alert)
self.presentViewController(self.uploadInProgressAlert, animated: true, completion: nil)

此代码用于关闭警报(警报不会被解除): self.uploadInProgressAlert.dismissViewControllerAnimated(false, completion: nil)

在这个帖子中: iOS解雇UIAlertController以回应有人谈到“持有引用”的事件。 我不知道“持有参考”是什么意思,我认为这可能是问题的根源。

编辑:我已将上述代码放在一个简单的测试应用程序中,并在那里工作。 但是当一些并行线程的事情变得复杂时,我找不到解除警报的方法。

var delay4s = NSTimer()
var delay8s = NSTimer()
var alert = UIAlertController()

func showAlert() {
    if NSClassFromString("UIAlertController") != nil {
        alert = UIAlertController(title: "Uploading", message: "Please wait! \n\n", preferredStyle: UIAlertControllerStyle.Alert)
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

func dismissAlert(){
    self.alert.dismissViewControllerAnimated(true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    delay4s = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: "showAlert", userInfo: nil, repeats: false)
    delay8s = NSTimer.scheduledTimerWithTimeInterval(8.0, target: self, selector: "dismissAlert", userInfo: nil, repeats: false)
}

通常,父视图控制器负责解除模态显示的视图控制器(您的弹出窗口)。 在Objective-C中,您可以在父视图控制器中执行以下操作:

[self dismissViewControllerAnimated:YES completion:nil];

Swift版本<3中的相同代码将是:

self.dismissViewControllerAnimated(true, completion: nil)

Swift 3.0:

self.dismiss(animated: true, completion: nil)

对于swift你可以这样做:

nameOfYourAlertController.dismiss(animated: true, completion: nil)

true将使消失动画,false将突然删除警报

如果要发布简要显示的警报,然后自行解除,则可以使用以下方法:

  func postAlert(title: String, message: String) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    self.present(alert, animated: true, completion: nil)

    // delays execution of code to dismiss
    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: {
      alert.dismiss(animated: true, completion: nil)
    })
  }

使用alertController自己的方法来销毁自己。

UIAlertController *alertController = [UIAlertController 
alertControllerWithTitle:...];

[alertController dismissViewControllerAnimated:YES completion:nil];

上面的任何内容似乎都不起作用,但这对我来说非常有用(xcode 10,swift 5)。 请享用!

第1步:放置这是您的viewController类

    var newQuestionAlert:UIAlertController?

第2步:创建显示警报的功能

  func ShowNewQuestionPopup() {
    if newQuestionAlert == nil {
        newQuestionAlert = UIAlertController(title: "Notice", message: "Next Question Starting", preferredStyle: .alert)
        if let newQuestionAlert = newQuestionAlert {
            newQuestionAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
                self.newQuestionAlert = nil
                return
            }))
            self.present(newQuestionAlert, animated: true, completion: nil)
         }
     }
 }

第3步:创建解除警报的功能

func autoDismiss() {
    newQuestionAlert?.dismiss(animated: false, completion: nil)
    newQuestionAlert = nil
}

第4步:根据需要调用函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM