一旦所有项目都出现在视图控制器中,就关闭 UIAlertView

Dismiss UIAlertView once all items appear in View Controller

我想在我的 table 视图控制器(从 EKEventStore)获取我的项目时显示一个 UIAlertView。一旦我的所有物品都出现了,我希望警报消失。

我尝试关闭 viewDidAppear() 中的警报,但这不起作用。我的警报一直存在。而且我不能在获取我的项目的代码之后就关闭它,因为获取在它自己的线程上运行并继续执行它之后的东西 - 因此,我的警报只会出现并立即消失。

let alert = UIAlertView(title: "Loading", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel")

override func viewDidLoad() {
    super.viewDidLoad()

    dispatch_async(dispatch_get_main_queue()) {
        let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37)) as UIActivityIndicatorView
        loadingIndicator.center = self.view.center;
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        loadingIndicator.startAnimating();

        self.alert.setValue(loadingIndicator, forKey: "accessoryView")
        loadingIndicator.startAnimating()
        self.alert.show()
    }

    // Code to fetch my items here...
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)       
    self.alert.dismissWithClickedButtonIndex(-1, animated: true)
}

它不应该在 viewDidAppear() 中。 viewDidAppear 每次您的视图出现时都会加载(这就是它立即关闭它的原因)。

你需要在你想要发生的一切都加载完成后,在你的代码中的某个地方关闭它。可能在这里:(但这取决于您的项目的具体提取方式)

// Code to fetch my items here...
self.alert.dismissWithClickedButtonIndex(-1, animated: true)
}