我想知道,是否可以检查用户是否打开了应用程序拥有的每个 viewcontroller
?
我愿意这样做,因为我给用户角标(Badge),这是我想给的。
我假设我必须将某些内容存储到 userDefaults
中并以某种方式收集信息,然后执行我想执行的操作,对吗?如果我是对的,那么我是否应该做一些全局变量并在每次用户打开新的 viewcontroller
时添加计数?
欢迎提供任何信息。
最佳答案
创建一个选项集来表示每个 viewController。在每个 viewControllers ViewDidAppear 中,从 Userdefaults 中读取并更新一个字段,该字段存储显示的 viewControllers 的选项集,然后将其写回 Userdefaults。
struct UserDefaultsKey {
static let displayedViewControllers = "displayedViewControllers"
}
struct DisplayedViewControllers: OptionSet {
let rawValue: Int
static let vc1 = DisplayedViewControllers(rawValue: 1 << 0)
static let vc2 = DisplayedViewControllers(rawValue: 1 << 1)
static let vc3 = DisplayedViewControllers(rawValue: 1 << 2)
static let vc4 = DisplayedViewControllers(rawValue: 1 << 3)
static let all = [vc1, vc2, vc3, vc4]
}
class vc1: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
var displayedViewControllers = DisplayedViewControllers(rawValue: UserDefaults.standard.integer(forKey: UserDefaultsKey.displayedViewControllers))
displayedViewControllers.insert(.vc1)
UserDefaults.standard.set(displayedViewControllers.rawValue, forKey: UserDefaultsKey.displayedViewControllers)
}
}
func haveAllViewControllersBeenDisplayed() -> Bool {
let displayedViewControllers = DisplayedViewControllers(rawValue: UserDefaults.standard.integer(forKey: UserDefaultsKey.displayedViewControllers))
for controller in DisplayedViewControllers.all {
if displayedViewControllers.contains(controller) == false {
return false
}
}
return true
}
关于ios - 检查用户是否打开了每个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40554643/