UISearchBar 在 Swift 完成后如何停止 UIActivityIndi​​cator

How to stop UIActivityIndicator after UISearchBar has finished in Swift

所以我的设置:

我有一个使用 Parse 设置的应用程序,我正在使用 PFQueryTableViewController。我在屏幕上添加了 UISearchBar。我已将此搜索全部设置好并且工作正常。

问题: 为了让用户更清楚地知道正在执行搜索,我想添加一个 UIActivityIndicator 以便他们可以看到加载。我也会 beginIgnoringInteractionEvents() 以便他们在搜索完成之前无法使用该应用程序。

我目前在如何让 UIActivityIndicator 停止动画方面遇到问题,endIgnoringInteractionEvents()- 我只是想不出我应该把这段代码放在哪里:

spinningActivityIndicator.stopAnimating() UIApplication.sharedApplication().endIgnoringInteractionEvents()

我已将 UIActivityIndicator 的代码放在方法 func searchBarSearchButtonClicked(searchBar: UISearchBar) 中,这样一旦他们单击键盘上的搜索,微调器就会启动。

但是我在哪里可以放置代码来停止动画并再次启用该应用程序?

我为UISearchBar添加的代码如下:

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

    searchBar.showsCancelButton = true

    self.loadObjects()

}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {

    // Dismiss the keyboard
    searchBar.resignFirstResponder()

    // Force reload of table data
    self.loadObjects()
}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {

    // Dismiss the keyboard
    searchBar.resignFirstResponder()

    spinningActivityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
    spinningActivityIndicator.center = self.view.center
    spinningActivityIndicator.hidesWhenStopped = true
    spinningActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    view.addSubview(spinningActivityIndicator)
    spinningActivityIndicator.startAnimating()
    UIApplication.sharedApplication().beginIgnoringInteractionEvents() 

    searchBar.showsCancelButton = false

    // Force reload of table data
    self.loadObjects()

}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {

    // Clear any search criteria
    searchBar.text = ""

    // Dismiss the keyboard
    searchBar.resignFirstResponder()

    // Force reload of table data
    self.loadObjects()
}

并在 func queryForTable() -> PFQuery 中添加:

    if filmSearchBar.text != "" {
        query.whereKey("FilmName", containsString: filmSearchBar.text!)

    }

提前致谢!

我刚刚查看了 PFQueryTableViewController 的一些文档,有一种方法 objectsDidLoad() 看起来很有希望。所以,尝试在你的 queryForTable() 函数下面添加这个:

override func objectsDidLoad(error: NSError?) {
    super.objectsDidLoad(error)
    // Your code here to stop the UIActivityIndicator
}

您可以在函数内添加一个 print("objectsDidLoad") 以查看它是否有效。