今天 UILabel 自动更新

Today UILabel auto updating

我在更新 'home' 视图控制器中的当前日期标签时遇到问题,如下所示:

func settingTodayLabel() {

  let todayFormatter = NSDateFormatter()
  todayFormatter.dateFormat = "d MMM YYYY"       
  todayLabel.text = todayFormatter.stringFromDate(NSDate()) 
}

此函数在 'ApplicationDidBecomeActive' 时调用,我希望在第二天从后台调用应用程序时更新 todayLabel 文本,但没有任何更改。显示旧日期。但是,如果我在设置中手动修改日期,它会起作用..

override func viewDidLoad() {        
super.viewDidLoad()

  NSNotificationCenter.defaultCenter().addObserver(self, selector: "settingTodayLabel", name: UIApplicationDidBecomeActiveNotification, object: nil)
}

UIApplicationDidBecomeActiveNotification 仅当应用程序激活时才会触发,因此您需要每天在 0:00 触发另一个通知来更新标签。

创建一个本地推送通知,在 0:00 触发并让它广播一个自定义的 NSNotification,您可以像

NSNotificationCenter.defaultCenter().addObserver(self, selector: "settingTodayLabel", name: "DateChangedNewDay", object: nil)

应用委托
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 代表 年 "Week of Year" 而年份不是从 1 月 1 日开始,但只在周之间切换:1 月的前 3 天可以属于最后一年,12 月的最后 3 天可以属于下一年。 y 代表我们所知的年份:从 1 月 1 日开始。