Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

ios - How to programmatically dismiss UIAlertController without any buttons?

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 to Amazon S3 (some things happen on parallel threads) and I'm afraid that the reference to the alert view controller gets lost when I want to dismiss it.

Any idea on what could be wrong? I even don't know how to debug this since there's no error in the Debug area?

I have a class level property: var uploadInProgressAlert = UIAlertController()

I use this code to present my alert without buttons (it works):

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

This code is to dismiss the alert (the alert doesn't get dismissed): self.uploadInProgressAlert.dismissViewControllerAnimated(false, completion: nil)

In this thread: iOS dismiss UIAlertController in response to event someone talked about "holding the reference". I don't know what it means "hold the reference" and I think this could be the root of the problem.

EDIT: I've put the above code in a simple test app and there it works. But when things get complicated with some parallel threads I can't find a way to dismiss the alert.

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

func showAlert() {
    if NSClassFromString("UIAlertController") != nil {
        alert = UIAlertController(title: "Uploading", message: "Please wait! 

", 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)
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...