簡體   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