日期更改时重新加载视图的方法
Method that reloads view when the day changes
我正在尝试创建一个在我的 SummaryViewController
后台运行的方法,它等待一天的改变,然后重新加载它自己的视图。
(顺便说一句,我等着看变化的日期是昨天...所以基本上当昨天变成今天时,我希望重新加载视图。所以 self.dateLabel.text!
等于昨天的日期。 )
这是我SummaryViewController
中的viewDidAppear()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// Background task that checks for when the day changes
while true {
sleep(1)
self.yesterday = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -2, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))
var yesterdayDateString = self.formatDate(self.yesterday!)
if (self.dateLabel.text! != yesterdayDateString) {
print("balls")
self.view.setNeedsDisplay()
}
}
}
我会使用 NSTimer 而不是 while 循环。
NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "check", userInfo: nil, repeats: true)
然后在您的 check 函数中,将当前日期与保存开始日期的全局变量进行比较,看看日期是否已更改。
我正在尝试创建一个在我的 SummaryViewController
后台运行的方法,它等待一天的改变,然后重新加载它自己的视图。
(顺便说一句,我等着看变化的日期是昨天...所以基本上当昨天变成今天时,我希望重新加载视图。所以 self.dateLabel.text!
等于昨天的日期。 )
这是我SummaryViewController
viewDidAppear()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// Background task that checks for when the day changes
while true {
sleep(1)
self.yesterday = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -2, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))
var yesterdayDateString = self.formatDate(self.yesterday!)
if (self.dateLabel.text! != yesterdayDateString) {
print("balls")
self.view.setNeedsDisplay()
}
}
}
我会使用 NSTimer 而不是 while 循环。
NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "check", userInfo: nil, repeats: true)
然后在您的 check 函数中,将当前日期与保存开始日期的全局变量进行比较,看看日期是否已更改。