如何在迅速
夫特3版本代码: 在您的第一视图控制器:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
}
func loadList(){
//load data here
self.tableView.reloadData()
}
在您的第二视图控制器:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
}
func loadList(notification: NSNotification){
//load data here
self.tableView.reloadData()
}
然后:
你可以这样做:
在你的tableView控制器
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
由于某种原因,这不适合我。我在'load data here'这一行添加了一个断点,但表格视图并没有显示添加的新数据 – 2015-03-08 22:39:31
谢谢,这个完美的解决方案,我正在寻找这样的一些东西。 – 2015-11-15 07:21:26
工作对我来说,我甚至从一个单独的类称为'postNotificationName'功能,甚至没有的ViewController – 2016-04-04 14:17:23
缺少逗号该线路上时,应改为:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"load", object: nil)
你可以使用NSNotificationCenter来触发一个更新你的tableview的方法。 – Sebastian 2014-09-18 20:24:53
你可以告诉我怎么样,我不是很熟悉NSNotificationCenter,谢谢 – 2014-09-18 20:28:21