UIAlertController 仅在特定时间
UIAlertController only in specific time
是否可以在不点击UIButton
的情况下仅在特定时间显示警报UIAlertController
?例如,当用户 运行 我的游戏第 3 次时。但如果他点击取消,他就再也看不到它了。
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let opensCount = UserDefaults.standard.integer(forKey: "open_count") + 1
UserDefaults.standard.set(opensCount, forKey: "open_count")
UserDefaults.standard.synchronize()
return true
}
和
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.integer(forKey: "open_count") == 3 {
self.showAlert()
}
...
}
是的,你可以添加一个整数来统计应用程序的打开次数并将其保存在用户默认值中,然后你检查它,如果它等于3,你显示警报,如果它被取消您保存值的用户也在用户默认值中以防止显示警报。
你的代码可以是这样的:
\this function to create the alert and present it
func showAlert(){
let alertController = UIAlertController(title: "Your Title", message: "Your Message", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
/*do your action here on cancel*/
}
let okAction = UIAlertAction(title: "Ok", style: .default) { _ in
/*do your action here on ok*/
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
在 viewdidload 调用中,您可以这样做:
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.integer(forKey: "open_count") == 3 {
self.showAlert()
}
}
您可以在您的应用程序委托中添加此代码,每次应用程序完成启动时都会调用此方法:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//initial count + 1 the count of this current open
let opensCount = UserDefaults.standard.integer(forKey: "open_count") + 1
//set the new count value
UserDefaults.standard.set(opensCount, forKey: "open_count")
//dont forget to synchronize your user defaults
UserDefaults.standard.synchronize()
return true
}
是否可以在不点击UIButton
的情况下仅在特定时间显示警报UIAlertController
?例如,当用户 运行 我的游戏第 3 次时。但如果他点击取消,他就再也看不到它了。
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let opensCount = UserDefaults.standard.integer(forKey: "open_count") + 1
UserDefaults.standard.set(opensCount, forKey: "open_count")
UserDefaults.standard.synchronize()
return true
}
和
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.integer(forKey: "open_count") == 3 {
self.showAlert()
}
...
}
是的,你可以添加一个整数来统计应用程序的打开次数并将其保存在用户默认值中,然后你检查它,如果它等于3,你显示警报,如果它被取消您保存值的用户也在用户默认值中以防止显示警报。
你的代码可以是这样的:
\this function to create the alert and present it
func showAlert(){
let alertController = UIAlertController(title: "Your Title", message: "Your Message", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
/*do your action here on cancel*/
}
let okAction = UIAlertAction(title: "Ok", style: .default) { _ in
/*do your action here on ok*/
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
在 viewdidload 调用中,您可以这样做:
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.integer(forKey: "open_count") == 3 {
self.showAlert()
}
}
您可以在您的应用程序委托中添加此代码,每次应用程序完成启动时都会调用此方法:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//initial count + 1 the count of this current open
let opensCount = UserDefaults.standard.integer(forKey: "open_count") + 1
//set the new count value
UserDefaults.standard.set(opensCount, forKey: "open_count")
//dont forget to synchronize your user defaults
UserDefaults.standard.synchronize()
return true
}