我认为每天当我打开它时,它会调用"viewWillAppear"但不是.当我在我的应用程序中更改某些内容,然后我向下滑动今天查看它有时会刷新视图,有时不会.
我在viewWillAppear中执行所有逻辑(从coreData获取数据并将该数据放到标签中),但不是每次都调用它.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
fetchContent()
setLabels()
setContentHeight()
tableView.reloadData()
print("view will appear")
}
Run Code Online (Sandbox Code Playgroud)
每次用户打开Today Extensions时如何调用fetchContent和setLabels?
Hug*_*nso 13
对于这件事,你应该使用NSWidgetProvinding
的widgetPerformUpdateWithCompletionHandler
.
脚步:
1.-确保你的UIViewController
工具NCWidgetProviding
class MainViewController: UIViewController, NCWidgetProviding
Run Code Online (Sandbox Code Playgroud)
2.-添加以下功能:
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)
}
Run Code Online (Sandbox Code Playgroud)
3.-在您的情况下,您将使用.NewData
.
只需确保检索所需数据并刷新视图(将每个数据放在适当的位置,填写标签,图表等).
没关系,在调用此函数期间您的视图不可见,iOS将填充视图并拍摄快照.
然后,这是在您打开通知中心时显示的内容,直到您再次获得对应用程序的控制权.
所以,在你的情况下会是这样的:
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
fetchContent()
setLabels()
setContentHeight()
tableView.reloadData()
completionHandler(NCUpdateResult.NewData)
}
Run Code Online (Sandbox Code Playgroud)