UIApplicationDidBecomeActiveNotification
будет срабатывать только тогда, когда приложение становится активным, поэтому вам нужно другое уведомление, что пожары каждый день в 0:00, чтобы обновить этикетку.
Создание локального уведомления толчка, что пожары в 0:00 и пусть транслировать пользовательский NSNotification, что ваш настроиться на как
NSNotificationCenter.defaultCenter().addObserver(self, selector: "settingTodayLabel", name: "DateChangedNewDay", object: nil)
AppDelegate
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationType = UIUserNotificationType.None
let settings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
application.registerUserNotificationSettings(settings)
var localNotification:UILocalNotification = UILocalNotification()
localNotification.fireDate = NSCalendar.currentCalendar().startOfDayForDate(NSDate())
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay // for testing: .CalendarUnitMinute
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
return true
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
NSNotificationCenter.defaultCenter().postNotificationName("DateChangedNewDay", object: nil)
}
}
ViewController
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var todayLabel: UILabel!
deinit{
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "settingTodayLabel", name: UIApplicationDidBecomeActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "settingTodayLabel", name: "DateChangedNewDay", object: nil)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.settingTodayLabel()
}
func settingTodayLabel() {
let todayFormatter = NSDateFormatter()
todayFormatter.dateFormat = "d MMM yyyy" // for testing: "d MMM yyyy HH:mm"
todayLabel.text = todayFormatter.stringFromDate(NSDate())
}
}
Я положил образец приложение онлайн, который обновляет каждую минуту: https://github.com/vikingosegundo/LabelUpdatingEachMinute
"d MMM YYYY"
должен, скорее всего, быть "d MM yyyy"
как Y
стенды для года «Неделя года» где год начинается не 1 января, но включается только между недель: первые 3 дня января могут принадлежать последнему году последних 3 дней декабря до следующего года. y
обозначает год, как мы его знаем: начинается 1 января.
попробуйте viewWillAppear вместо viewDidLoad –
@LeonardoSavioDabus, это также не будет работать для случая кромки, когда вид открывается при изменении дня. – vikingosegundo
@vikingsegundo хорошая точка –