启动时,打开指定的ViewController (iOS)
At startup, open the specified ViewController (iOS)
我有两个 VC(ViewController、SettingsVC、VC2)。
如何才能让当你打开开关(位于SettingsVC
),应用程序启动时,它会显示VC2?
默认为ViewController.swift.
我试过这段代码,但是当我打开开关时,重启后没有任何反应。
SettingsVC.swift
let isSwitchOn = UserDefaults.standard.set(true, forKey: "isSwitchOn")
@IBOutlet weak var `switch`: UISwitch!
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let isSwitchOn = UserDefaults.standard.bool(forKey: "isSwitchOn")
if isSwitchOn {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let VC2 = storyboard.instantiateViewController(withIdentifier: "ViewController")
self.window!.rootViewController = VC2;
}
// Override point for customization after application launch.
return true
}
当开关改变时,您不改变 UserDefaults
值。您应该将以下 IBAction
连接到 IB 中的 Value Changed:
SettingsVC.swift
@IBAction func switchValueChanged(_ sender: UISwitch) {
UserDefaults.standard.set(sender.isOn, forKey: "isSwitchOn")
}
我有两个 VC(ViewController、SettingsVC、VC2)。
如何才能让当你打开开关(位于SettingsVC
),应用程序启动时,它会显示VC2?
默认为ViewController.swift.
我试过这段代码,但是当我打开开关时,重启后没有任何反应。
SettingsVC.swift
let isSwitchOn = UserDefaults.standard.set(true, forKey: "isSwitchOn")
@IBOutlet weak var `switch`: UISwitch!
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let isSwitchOn = UserDefaults.standard.bool(forKey: "isSwitchOn")
if isSwitchOn {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let VC2 = storyboard.instantiateViewController(withIdentifier: "ViewController")
self.window!.rootViewController = VC2;
}
// Override point for customization after application launch.
return true
}
当开关改变时,您不改变 UserDefaults
值。您应该将以下 IBAction
连接到 IB 中的 Value Changed:
SettingsVC.swift
@IBAction func switchValueChanged(_ sender: UISwitch) {
UserDefaults.standard.set(sender.isOn, forKey: "isSwitchOn")
}