How to programmatically dismiss UIAlertController without any buttons?

前端 未结 5 1531
[愿得一人]
[愿得一人] 2020-12-09 07:52

I\'m presenting an UIAlertViewController without any buttons, as it is supposed to just inform users that uploading is in progress. The app is supposed to upload some files

相关标签:
5条回答
  • 2020-12-09 08:21

    If you want to post an alert that displays briefly, then dismisses itself, you could use the following method:

      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)
        })
      }
    
    0 讨论(0)
  • 2020-12-09 08:21

    Use the alertController's own method to destroy itself.

    UIAlertController *alertController = [UIAlertController 
    alertControllerWithTitle:...];
    
    [alertController dismissViewControllerAnimated:YES completion:nil];
    
    0 讨论(0)
  • 2020-12-09 08:26

    for swift you can do:

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

    true will animate the disappearance, and false will abruptly remove the alert

    0 讨论(0)
  • 2020-12-09 08:27

    Generally the parent view controller is responsible for dismissing the modally presented view controller (your popup). In Objective-C you would do something like this in the parent view controller:

    [self dismissViewControllerAnimated:YES completion:nil];
    

    The same code in Swift versions < 3 would be:

    self.dismissViewControllerAnimated(true, completion: nil)
    

    Swift 3.0:

    self.dismiss(animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-09 08:27

    Nothing above seemed to work, but here is what works for me perfectly (xcode 10, swift 5). Enjoy!

    Step 1: Place this is your viewController Class

        var newQuestionAlert:UIAlertController?
    

    Step 2: Create function to show alert

      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)
             }
         }
     }
    

    Step 3: Create function to dismiss alert

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

    Step 4: Call functions as needed

    0 讨论(0)
提交回复
热议问题