因为tabbar页面切换是不走
override func viewDidLoad() 的
只有你第一次切换的时候会走这个,之后再切换回你tabbar里面的这个页面的时候是不会走这个方法的。
所以你需要重写viewWillAppear ! 举个例子,重写的写法:
你原本只有这个viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = 80
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "You")
do {
let result = try managedObjectContext.fetch(request)
allScore = result as! [You]
} catch {
print("取出失败")
}
if(allScore.isEmpty){
let item: You
item = NSEntityDescription.insertNewObject(forEntityName: "You", into: managedObjectContext) as! You
item.totalScore = 0
print("是空的,要再加一下")
}
fetchYou()
fetchAllRewars()
totalScore.title = String(allScore[0].totalScore)
}
我需要在这个代码的页面的上面写一个
override func viewWillAppear(_ animated: Bool) {
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "You")
do {
let result = try managedObjectContext.fetch(request)
allScore = result as! [You]
} catch {
print("取出失败")
}
if(allScore.isEmpty){
let item: You
item = NSEntityDescription.insertNewObject(forEntityName: "You", into: managedObjectContext) as! You
item.totalScore = 75
}
print("第一个有运行")
fetchYou()
totalScore.title = String(allScore[0].totalScore)
}
然后每次切换tabbar里面的东西的时候就可以刷新了!!
|