我有Xcode 8.2,iOS 10,Swift 3.如何在運行耗時功能時顯示活動指示器警報控制器?
在我的應用程序中,用戶點擊一個按鈕「開始處理」,它啓動了一個耗時的功能。我希望有一個包含活動指示器的警報窗口。然而,我所看到的所有教程都告訴我如何啓動和停止它,而不是如何將它與運行一個函數異步配對。
我的代碼是這樣的:
func doProcessing() {
for (...) {
timeConsumingFunction()
}
}
// This function displays a setup which allows the user to manually kick off the time consuming processing.
func displaySetupScreen() {
let alertController = UIAlertController(title: "Settings", message: "Please enter some settings.", preferredStyle: .alert)
// ask for certain settings, blah blah.
let actionProcess = UIAlertAction(title: "Process", style: .default) { (action:UIAlertAction) in
//This is called when the user presses the "Process" button.
let textUser = alertController.textFields![0] as UITextField;
self.doProcessing()
// once this function kicks off, I'd like there to be an activity indicator popup which disappears once the function is done running.
}
self.present(alertController, animated: true, completion: nil)
}
// this displays the actual activity indicator ... but doesn't work
func displayActivityIndicator() {
// show the alert window box
let alertController = UIAlertController(title: "Processing", message: "Please wait while the photos are being processed.", preferredStyle: .alert)
let activityIndicator : UIActivityIndicatorView = UIActivityIndicatorView()
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
activityIndicator.startAnimating()
self.present(alertController, animated: true, completion: nil)
}
基本上,我不知道如何啓動和停止在正確的時間將活動的指標,我怎麼能顯示在這段時間警報控制器。
謝謝你的幫助。
看看這個問題http://stackoverflow.com/questions/2919879/uiactivityindicator-not-working-properly – ebby94
http://stackoverflow.com/questions/27033466/how-to-display-activity-indicator -in-center-of-uialertcontroller http://stackoverflow.com/questions/31408319/how-do-i-put-a-uiactivityindicatorview-in-a-uialertcontroller – 2017-02-12 02:37:46
你可以使用NSTImer來設置你的活動指示器的時間開始和停止。 –