Swift UIPageViewController & UIActivityIndi​​catorView

Swift UIPageViewController & UIActivityIndicatorView

是否有人知道停止 UIActivityIndi​​catorView (AI) / 删除已添加到 UIPageViewController 的 UIView 的问题?

我正在展示 AI 使用:

override func viewWillLayoutSubviews() {
    actInd.frame = CGRectMake(0,0, 80, 80)
    actInd.center.x = self.view.center.x
    actInd.center.y = self.view.center.y - 40
    actInd.hidesWhenStopped = true
    actInd.layer.cornerRadius = 5
    actInd.alpha = 0.5
    actInd.backgroundColor = UIColor.blackColor()
    actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    self.view.addSubview(actInd)
    actInd.startAnimating()

    // This next line prints out the details of each subview on the page for testing purposes
    listSubviews(self.view)

出于测试目的,我还使用以下方法向视图添加了一个空白的 UIView:

    aView.frame = CGRectMake(0, 0, 200, 200)
    aView.backgroundColor = UIColor.blueColor()
    self.view.addSubview(aView)
}

然后我使用委托发送我的 Parse.com 查询,以在数据准备好时通知 UIPageViewController(例如此视图)。这工作正常,我已经测试数据 returns 正确,并且正在调用预期的方法。

在这个方法中,我尝试停止 UIActivityViewController 并尝试隐藏 aView:

self.actInd.stopAnimating()
aView.removeFromSuperView()

这没有用,所以我尝试了:

self.actInd.removeFromSuperView()

这也没有用。所以我然后尝试使用以下方法搜索当前视图的子视图:

func populateBoards(boards: [Board]) {
    println(self.actInd) // PRINT LINE 1

    var subviews = self.view.subviews

    for v in subviews {

        if v.isKindOfClass(UIActivityIndicatorView) {
            println("YES, it is an activity view indicator \(v)") // PRINT LINE 2
            v.stopAnimating()
            v.removeFromSuperview()
        }

    }

    self.boards = boards
    println("Populated Boards!") // PRINT LINE 3
}

PRINT LINE 1 输出:>

PRINT LINE 2 输出:是,它是一个 activity 视图指示器 >

PRINT LINE 3 输出:"Populated boards!"

编辑:

UIPageViewController 使用以下代码设置(以防有帮助):

func setupUIPageView() {

    self.pageViewController = UIPageViewController(transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: nil)
    self.pageViewController.delegate = self
    self.pageViewController.dataSource = self

    var startVC = self.viewControllerAtIndex(0) as ViewController
    var viewControllers:[ViewController] = [startVC]

    self.pageViewController.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)

    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)

    self.pageViewController.view.alpha = 0.0

    self.addChildViewController(self.pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)

}

求助!提前谢谢大家 :D

您应该在主队列中调用所有 UI 相关代码。您的 Parse 查询是异步的,可能会在不同的线程上调用完成块。

在此块中包装此调用:

NSOperationQueue.mainQueue().addOperationWithBlock {
  self.actInd.stopAnimating()
}