【发布时间】:2017-02-12 02:06:41
【问题描述】:
我有 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)
}
基本上,我不知道如何在正确的时间启动和停止活动指示器,以及如何在此期间显示警报控制器。
感谢您的帮助。
【问题讨论】:
-
您可以使用 NSTImer 设置活动指示器的开始和停止时间。
标签: ios swift swift3 uiactivityindicatorview