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

And*_*rej 33 cocoa-touch ios swift

我正在展示一个没有任何按钮的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)
Run Code Online (Sandbox Code Playgroud)

此代码用于关闭警报(警报不会被解除): 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)
}
Run Code Online (Sandbox Code Playgroud)

Jiu*_*der 55

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

[self dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

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

self.dismissViewControllerAnimated(true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

Swift 3.0:

self.dismiss(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

  • 是的,这实际上指出了我正确的方向.我不得不使用`self.dismissViewControllerAnimated(false,completion:nil)`而不是`self.uploadInProgressAlert.dismissViewControllerAnimated(false,completion:nil)`.但是我不会忘记它为什么会起作用,但它确实有效. (3认同)
  • 它的工作原理是因为@MySpecialPurpose,父视图控制器负责解除任何呈现的视图控制器 (3认同)
  • 如果父母也是“模态”怎么办? (3认同)

小智 9

对于swift你可以这样做:

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

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


Adr*_*ian 7

如果要发布短暂显示的警报,然后将其自行关闭,则可以使用以下方法:

  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)
    })
  }
Run Code Online (Sandbox Code Playgroud)


ylg*_*hyh 6

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

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

[alertController dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)