每次用户打开应用程序时如何调用函数

时间:2016-07-21 07:21:39

标签: ios swift

每次用户点击我的应用程序图标并打开它时,我想调用名为update的函数。我该怎么办?无论何时应用程序启动,我都可以覆盖哪种方法可以覆盖后台或用户按下主页按钮。

 import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    @IBOutlet weak var button: UIButton!

    @IBOutlet weak var labeltwo: UILabel!

    var NSDateDefalt = NSUserDefaults.standardUserDefaults()
    var date : NSDate?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        date = NSUserDefaults.standardUserDefaults().objectForKey("yourKey") as? NSDate
        label.text = "\(date)"
        update()
    }


    @IBAction func buttona(sender: AnyObject) {
        date = NSDate()
        label.text = "\(date)"

        NSUserDefaults.standardUserDefaults().setObject(NSDate(), forKey:"yourKey")
    }



    func update(){
        let now = NSDate()
        let seconds = now.timeIntervalSinceDate(date!)
        labeltwo.text = "\(seconds))"
    }

}

更新:我在我的App Delegate

中实施
 func applicationWillEnterForeground(application: UIApplication) {
        ViewController().update()     
    }

6 个答案:

答案 0 :(得分:12)

您需要在AppDelegate中实现您的逻辑

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

        // first launch
        // this method is called only on first launch when app was closed / killed
        return true
    }

    func applicationWillEnterForeground(application: UIApplication) {

        // app will enter in foreground
        // this method is called on first launch when app was closed / killed and every time app is reopened or change status from background to foreground (ex. mobile call)
    }

    func applicationDidBecomeActive(application: UIApplication) {

        // app becomes active
        // this method is called on first launch when app was closed / killed and every time app is reopened or change status from background to foreground (ex. mobile call)
    }
}

<强>更新

正如Paulw11所建议的那样,考虑使用applicationWillEnterForeground而不是applicationDidBecomeActive

答案 1 :(得分:7)

ViewController.swiftviewDidLoad()以相应方式编写其中任何一个:

NotificationCenter.default.addObserver(self, selector: #selector(self.update), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.update), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)

(编辑:Swift 3更新)

(编辑:删除了这篇文章中不正确的部分内容.iOS会自动向任何添加的观察者触发这些通知,无需在App Delegate中添加代码来触发它们)

答案 2 :(得分:6)

在您的AppDelegate.swift中,有didFinishLaunchingWithOptionsapplicationWillEnterForeground

applicationWillEnterForeground与您的viewControllers相比,viewWillAppear与您的应用相似,而didFinishLaunchingWithOptions类似于您应用的viewDidLoad。有关详细信息,请查看UIApplicationDelegate

答案 3 :(得分:2)

使用UIApplicationDidBecomeActive通知。

答案 4 :(得分:2)

当应用程序处于已终止状态且用户点击应用程序图标时,首先调用的方法是

didFinishLaunchingWithOptions 

当应用程序处于后台模式并进入前台时

applicationWillEnterForeground

所以你可以在两个方法中调用Update()。但是当didFinishLaunchingWithOptions调用的方法不在applicationWillEnterForeground中调用时。因为当app启动时调用这两个方法,当app进入前台模式时,只有WillEnterForeground方法调用。这可以通过bool flag来管理

答案 5 :(得分:2)

对于 Swift 5 ,请使用此代码

  1. func viewDidLoad()

    中添加此行

    NotificationCenter.default.addObserver(self, selector: #selector(self.updateView), name: UIApplication.didBecomeActiveNotification, object: nil)

  2. 添加此功能

    @objc func updateView(){

    }