Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 运行耗时功能时,如何显示活动指示器警报控制器?_Ios_Swift_Swift3_Uiactivityindicatorview - Fatal编程技术网

Ios 运行耗时功能时,如何显示活动指示器警报控制器?

Ios 运行耗时功能时,如何显示活动指示器警报控制器?,ios,swift,swift3,uiactivityindicatorview,Ios,Swift,Swift3,Uiactivityindicatorview,我有Xcode 8.2、iOS 10和Swift 3 在我的应用程序中,用户单击“开始处理”按钮,启动一个耗时的功能。我希望有一个包含活动指示器的警报窗口。然而,我看到的所有教程都只是告诉我如何启动和停止它,而不是如何将它与函数的运行异步配对 我的代码是这样的: func doProcessing() { for (...) { timeConsumingFunction() } } // This function displays a setup which

我有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)
}
activityIndicator.startAnimating()
DispatchQueue.main.async {
  //Put your long-running code here
  activityIndicator.stopAnimating()
}
基本上,我不知道如何在正确的时间启动和停止活动指示器,以及如何在此期间显示警报控制器


感谢您的帮助。

正如他在评论中发布的链接ebby94所说,您应该真正避免在主线程上运行耗时的任务。它会冻结用户界面,如果你花的时间太长,系统跳板最终会以挂起状态终止你的应用程序

您应该真正在后台任务上运行长期运行的任务。如果没有更多的信息,我真的无法详细解释

如果决定在主线程上运行耗时的任务,则需要启动活动指示器旋转,然后返回并给事件循环时间,以便在任务开始之前实际启动动画。大概是这样的:

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)
}
activityIndicator.startAnimating()
DispatchQueue.main.async {
  //Put your long-running code here
  activityIndicator.stopAnimating()
}

Dispatch中的代码仍将在主线程上运行,但首先运行循环将有机会启动活动指示器。

正如他在评论中发布的链接ebby94所说,您应该真正避免在主线程上运行耗时的任务。它会冻结用户界面,如果你花的时间太长,系统跳板最终会以挂起状态终止你的应用程序

您应该真正在后台任务上运行长期运行的任务。如果没有更多的信息,我真的无法详细解释

如果决定在主线程上运行耗时的任务,则需要启动活动指示器旋转,然后返回并给事件循环时间,以便在任务开始之前实际启动动画。大概是这样的:

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)
}
activityIndicator.startAnimating()
DispatchQueue.main.async {
  //Put your long-running code here
  activityIndicator.stopAnimating()
}

分派中的代码仍将在主线程上运行,但首先,运行循环将有机会启动活动指示器。

查看此问题您可以使用NSTImer设置活动指示器的启动和停止时间。查看此问题您可以使用NSTImer设置活动指示器的启动和停止时间。谢谢。在Swift 3中,它应该是
DispatchQueue.main.async
,但这个答案对我来说很有用。很抱歉输入错误。我凭记忆打出来的,显然是搞错了。很高兴你能纠正这个错误。谢谢。在Swift 3中,它应该是
DispatchQueue.main.async
,但这个答案对我来说很有用。很抱歉输入错误。我凭记忆打出来的,显然是搞错了。很高兴你能纠正这个错误。