我以为每次打开“今日视图”,它都叫"viewWillAppear“,但它不是。当我在我的应用程序中改变一些东西,然后我滑落到“今日视图”,它有时刷新视图,有时不刷新视图。
我执行viewWillAppear中的所有逻辑(从coreData中获取数据并将数据放到标签中),但它不是每次都被调用的。
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
fetchContent()
setLabels()
setContentHeight()
tableView.reloadData()
print("view will appear")
}当用户每次打开“今日扩展”时如何调用fetchContent和setLabels?
发布于 2016-01-21 21:39:54
在这件事上,你应该使用NSWidgetProvinding的widgetPerformUpdateWithCompletionHandler。
步骤:
1.-确保UIViewController实现NCWidgetProviding
class MainViewController: UIViewController, NCWidgetProviding2.-增加以下功能:
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData
completionHandler(NCUpdateResult.NewData)
}3.-在您的情况下,您将使用.NewData。
只需确保检索所需的数据并刷新视图(将所有数据放在适当的位置,填充标签、图表等)。
不要担心在调用此函数期间,视图是不可见的,iOS将填充视图并对其进行快照。
这就是当你打开通知中心的时候所显示的,直到你再次控制你的应用程序。
所以,在你的例子中,应该是这样的:
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
fetchContent()
setLabels()
setContentHeight()
tableView.reloadData()
completionHandler(NCUpdateResult.NewData)
}发布于 2016-01-22 09:48:41
Swift 2.1 && Xcode 7.2 --当您多次重新编译这个扩展时,似乎出现了一些错误。解决方案是从通知中心移除并再次添加它。然后每次打开它都会刷新。
https://stackoverflow.com/questions/34929954
复制相似问题