我想在swift中每隔10次显示一个警报

问题描述 投票:2回答:2

下午好,没有找到具体的答案,我决定自己问他我需要每隔10次打开一个警报。

ios swift2
2个回答
1
投票

我的意思是你想在应用程序打开时每隔10次显示警报,你可以这样做:在你的AppDelegate中,在UserDefaults中保存计数Int并在每次应用程序打开时递增它。然后在10的倍数时检查数字。

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
      ...
    func showAlert(count:Int) {
        let alert = UIAlertController(title: "Title", message: "You have open the app \(count) times", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        // show the alert
        window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
    }

    let defaults = NSUserDefaults.standardUserDefaults()
    var  count = defaults.integerForKey("count") ?? 0
    count += 1
    defaults.setInteger(count, forKey: "count")
    if count % 10 == 0 {  // each 10th time the app opens
        showAlert(count)
    }
   ...
}

2
投票

使用application: didFinishLaunchingWithOptions记录应用程序在NSUserDefaults中启动的次数,并在第10次显示警报。

© www.soinside.com 2019 - 2024. All rights reserved.