Daniel
Daniel

Reputation: 3864

Dismiss UIAlertView once all items appear in View Controller

I would like to show a UIAlertView while my items are being fetched (from the EKEventStore) by my table view controller. Once all my items have appeared I would like the alert to disappear.

I tried dismissing the alert in viewDidAppear() but that doesn't work. My alert just stays around. And I can't just dismiss it after the code to fetch my items them because the fetching runs on its own thread and continues executing stuff after it - hence, my alert will just appear and immediately disappear.

let alert = UIAlertView(title: "Loading", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel")

override func viewDidLoad() {
    super.viewDidLoad()

    dispatch_async(dispatch_get_main_queue()) {
        let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37)) as UIActivityIndicatorView
        loadingIndicator.center = self.view.center;
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        loadingIndicator.startAnimating();

        self.alert.setValue(loadingIndicator, forKey: "accessoryView")
        loadingIndicator.startAnimating()
        self.alert.show()
    }

    // Code to fetch my items here...
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)       
    self.alert.dismissWithClickedButtonIndex(-1, animated: true)
}

Upvotes: 0

Views: 427

Answers (1)

William GP
William GP

Reputation: 1352

It shouldn't be in viewDidAppear(). viewDidAppear loads everytime your view appears (which is why it is dismissing this right away).

You need to instead dismiss this somewhere in your code after everything you want to happen is done loading. Possibly here: (but that depends on what exactly the fetch of your items looks like)

// Code to fetch my items here...
self.alert.dismissWithClickedButtonIndex(-1, animated: true)
}

Upvotes: 1

Related Questions