一旦我的应用程序即将进入前台,我希望将我的TableView设置为reloadData。我已经完成了以下操作,但它不起作用:
override func viewWillAppear(_ animated: Bool) {
//registering ContactsVC as an Observer
NotificationCenter.default.addObserver(self,selector: #selector(willEnterForeground),name: NSNotification.Name.UIApplicationWillEnterForeground,object: nil)
}
override func viewDidDisappear(_ animated: Bool) {
//remove ContactsVC as the observer when your view is going away
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}
@objc func willEnterForeground() {
print("print if willEnterForeground() is called")
self.tableView.reloadData()
}我的函数处理程序中的print语句可以在控制台上打印出来,但是tableview不会重新加载。
发布于 2018-07-06 22:58:44
Swift 4.1
在AppDelegate中
func applicationWillEnterForeground(_ application: UIApplication) {
NotificationCenter.default.post(name: NSNotification.Name("ReloadNotification"), object: nil)
}然后转到所需的视图控制器,并在ViewDidload中
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData(_:)), name: NSNotification.Name("ReloadNotification"), object: nil)最后,在ViewController中添加通知函数
@objc func reloadData(_ notification: Notification?) {
self.tableview.reloadData()
}发布于 2020-05-25 14:37:31
在ViewDidLoad中,
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)
}在Deinit中,删除观察者,
deinit() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: UIApplication.willEnterForegroundNotification), object: nil)
}在主派发队列中重新加载该表,
@objc func reloadData(_ notification: Notification?) {
DispatchQueue.main.async {
[weak self] in
self?.tableview.reloadData()
}
}发布于 2018-01-04 18:58:09
转到AppDelegate并添加通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationToReload" object:nil];在……里面
- (void)applicationDidBecomeActive:(UIApplication *)application然后转到所需的视图控制器,并在ViewDidload中添加相同的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(forceUpdateSkip:) name:@"NotificationToReload" object:nil ];然后在相同的ViewController中添加通知函数
-(void)forceUpdateSkip:(NSNotification*)notification{
[tableview reloadData];
}请替换您表名,而不是tableview。
现在,只要应用程序进入前台,通知就会被调用,表就会被重新加载。
或
遵循此选项,
只需添加
[tablename reloadData];在……里面
-(void)viewWillAppear:(BOOL)animatedhttps://stackoverflow.com/questions/48080119
复制相似问题