更改 ViewController 并重新启动应用程序
Change the ViewController and restart app
我制作了两个 ViewController,如何将 rootViewController 从 ViewController() 更改为 MainView()?这是我在 SceneDelegate
中的代码
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
window?.backgroundColor = UIColor.black
if isNewUser == true {
window?.rootViewController = ViewController()
} else {
window?.rootViewController = MainView()
}
当我 运行 应用程序测试 isNewUser = true 和 isNewUser = false 时,这工作正常。
在我的ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Show the welcome screen, blah.
//Accept the location, blah.
//After accepted everything, change isNewUser to false in UserDefault.Standard
print("App is restart")
}
}
class MainView: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
print("Welcome to AiOO!")
}
}
两者都工作正常,但是例如,当我完成接受协议或接受位置时,第一次出现类似的情况 运行 应用程序...它将在 [=34 中将 isNewUser 设置为 false =].所以只有一件事我卡住了,我怎样才能重新启动应用程序或将 rootViewController 切换到 MainView()??
更新:
RestartApp,当我点击 "Complete" 按钮时没有运气,没有 UITabBarControl 并且在我杀死应用程序并再次打开之前不会调用 MainView,它工作:( 下面是我的完整代码:
在 SceneDelegate 中:
var versioncheck = "1.0.0.0"
class SceneDelegate: UIResponder, UIWindowSceneDelegate, RestartApp {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
sceneRestore()
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
window?.backgroundColor = UIColor(named: "Main Background")
restartApp()
}
func restartApp() {
if !UserDefaults.standard.bool(forKey: "New User") {UserDefaults.standard.set(true, forKey: "New User")}
let isNewUser = UserDefaults.standard.bool(forKey: "New User")
print(isNewUser) // It will keep say "true" even change to false in UserDefault
if isNewUser == true {
window?.rootViewController = ViewController()
} else if versioncheck != "1.0.0.0" {
window?.rootViewController = NewUpdate()
} else {
window?.rootViewController = MainView()
}
}
}
ViewController
protocol RestartApp {
func restartApp()
}
class ViewController: UIViewController {
var delegate: RestartApp?
override func viewDidLoad() {
super.viewDidLoad()
welcomePage()
}
}
当我点击"Complete"按钮时,它会调用:
@objc func welcomeChangeGoToMain () {
UserDefaults.standard.set(false, forKey: "New User")
print(UserDefaults.standard.bool(forKey: "New User")) //It said "false"
welcomeTitle.removeFromSuperview()
welcomeSubline.removeFromSuperview()
welcomeNext.removeFromSuperview()
welcomeRequest.removeFromSuperview()
self.delegate?.restartApp()
}
您可以创建一个委托协议,只要 isNewUser 值发生变化就会调用它,并在场景中调用它的委托函数 class 以便根据该值呈现视图控制器。喜欢下面:
在你里面 SceneDelegate class :
// don't forget to assign the RestartApp Delegate here
class SceneDelegate: UIResponder, UIWindowSceneDelegate, RestartApp {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
restartApp()
}
并在 sceneDelegate 中创建 restartApp 函数
func restartApp() {
let isNewUser = UserDefaults.standard.bool(forKey: "Key")
if isNewUser == true {
window?.rootViewController = ViewController()
} else {
window?.rootViewController = ViewController2()
}
}
在您希望重启函数被调用的最后创建协议,它的委托调用如下所示:
protocol RestartApp {
func restartApp()
}
class ViewController2: UIViewController {
var delegate: RestartApp?
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.set(true, forKey: "Key")
view.backgroundColor = .yellow
print("VC2")
self.delegate?.restartApp() // this should be called wherever you want to restart the app ( inside an action or a function depends on your need )
}
}
总而言之,这里发生了什么,应用程序启动它调用 SceneDelegate class 中的 willConnectTo 函数,这个调用重新启动应用程序并根据 IsNewUser 中保存的最后一个值调用 VC ,然后在你的内部 VC 每次调用 self.delegate.restartApp() 时,它都会调用 SceneDelegate 中的重启函数,并根据你的 isNewUser 值调用 VC 。
我制作了两个 ViewController,如何将 rootViewController 从 ViewController() 更改为 MainView()?这是我在 SceneDelegate
中的代码guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
window?.backgroundColor = UIColor.black
if isNewUser == true {
window?.rootViewController = ViewController()
} else {
window?.rootViewController = MainView()
}
当我 运行 应用程序测试 isNewUser = true 和 isNewUser = false 时,这工作正常。
在我的ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Show the welcome screen, blah.
//Accept the location, blah.
//After accepted everything, change isNewUser to false in UserDefault.Standard
print("App is restart")
}
}
class MainView: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
print("Welcome to AiOO!")
}
}
两者都工作正常,但是例如,当我完成接受协议或接受位置时,第一次出现类似的情况 运行 应用程序...它将在 [=34 中将 isNewUser 设置为 false =].所以只有一件事我卡住了,我怎样才能重新启动应用程序或将 rootViewController 切换到 MainView()??
更新: RestartApp,当我点击 "Complete" 按钮时没有运气,没有 UITabBarControl 并且在我杀死应用程序并再次打开之前不会调用 MainView,它工作:( 下面是我的完整代码:
在 SceneDelegate 中:
var versioncheck = "1.0.0.0"
class SceneDelegate: UIResponder, UIWindowSceneDelegate, RestartApp {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
sceneRestore()
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
window?.backgroundColor = UIColor(named: "Main Background")
restartApp()
}
func restartApp() {
if !UserDefaults.standard.bool(forKey: "New User") {UserDefaults.standard.set(true, forKey: "New User")}
let isNewUser = UserDefaults.standard.bool(forKey: "New User")
print(isNewUser) // It will keep say "true" even change to false in UserDefault
if isNewUser == true {
window?.rootViewController = ViewController()
} else if versioncheck != "1.0.0.0" {
window?.rootViewController = NewUpdate()
} else {
window?.rootViewController = MainView()
}
}
}
ViewController
protocol RestartApp {
func restartApp()
}
class ViewController: UIViewController {
var delegate: RestartApp?
override func viewDidLoad() {
super.viewDidLoad()
welcomePage()
}
}
当我点击"Complete"按钮时,它会调用:
@objc func welcomeChangeGoToMain () {
UserDefaults.standard.set(false, forKey: "New User")
print(UserDefaults.standard.bool(forKey: "New User")) //It said "false"
welcomeTitle.removeFromSuperview()
welcomeSubline.removeFromSuperview()
welcomeNext.removeFromSuperview()
welcomeRequest.removeFromSuperview()
self.delegate?.restartApp()
}
您可以创建一个委托协议,只要 isNewUser 值发生变化就会调用它,并在场景中调用它的委托函数 class 以便根据该值呈现视图控制器。喜欢下面:
在你里面 SceneDelegate class :
// don't forget to assign the RestartApp Delegate here
class SceneDelegate: UIResponder, UIWindowSceneDelegate, RestartApp {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
restartApp()
}
并在 sceneDelegate 中创建 restartApp 函数
func restartApp() {
let isNewUser = UserDefaults.standard.bool(forKey: "Key")
if isNewUser == true {
window?.rootViewController = ViewController()
} else {
window?.rootViewController = ViewController2()
}
}
在您希望重启函数被调用的最后创建协议,它的委托调用如下所示:
protocol RestartApp {
func restartApp()
}
class ViewController2: UIViewController {
var delegate: RestartApp?
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.set(true, forKey: "Key")
view.backgroundColor = .yellow
print("VC2")
self.delegate?.restartApp() // this should be called wherever you want to restart the app ( inside an action or a function depends on your need )
}
}
总而言之,这里发生了什么,应用程序启动它调用 SceneDelegate class 中的 willConnectTo 函数,这个调用重新启动应用程序并根据 IsNewUser 中保存的最后一个值调用 VC ,然后在你的内部 VC 每次调用 self.delegate.restartApp() 时,它都会调用 SceneDelegate 中的重启函数,并根据你的 isNewUser 值调用 VC 。