问题
I am trying to change the rootview controller to a different screen after the user has logged out.
if let window = UIApplication.shared.windows.first {
window.rootViewController = UIHostingController(rootView: SignInView())
window.endEditing(true)
window.makeKeyAndVisible()
}
This is what I have so far. It navigates to the different view but it makes the buttons on the new new view unusable. Is there a better way to change the rootview or am I missing something?
回答1:
My approach is to create an observable object that coordinates navigation between the various app views. Below a simplified example to give you an idea:
//Your app views
enum AppViews {
case LoginView
case MainAppView
}
//Class that conforms to the ObservableObject protocol and publishes the viewId property.
//Basically your navigation will react to viewId changes
class ShowingView: ObservableObject {
init(showingView: AppViews) {
self.viewId = showingView
}
@Published var viewId : AppViews
}
//The current root view of your app is observing changes of the ShowingView class and switch between your app views
struct AppRootView: View {
@ObservedObject var showingView: ShowingView
var body: some View {
Group {
if showingView.viewId == .LoginView {
TermsAndConditionsView()
.environmentObject(showingView)
}
else {
MainAppView()
.environmentObject(showingView)
}
}
}
}
Notice that you can pass showingView as environmentObject to your views and use it to switch to another view when required. In your case switch to another screen when the user logs out.
In your scene delegate you can then for example switch between login and main view as follows
var appStartView: AppViews
let isloggedIn = UserDefaults.standard.bool(forKey: "IsLoggedIn")
if isLoggedIn == false {
appStartView = .LoginView
}
else {
appStartView = .MainAppView
}
let contentView = AppRootView(showingView: ShowingView(showingView: appStartView))
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
To switch between MainAppView and LoginView you can simply modify the value of the environmentObject showingView
self.showingView.viewId = AppViews.LoginView
I hope it helps to guide you in the right direction
回答2:
If are you using SceneDelegate.swift file in your project then try this way.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
// Instantiate UIWindow with scene
let window = UIWindow(windowScene: scene)
// Assign window to SceneDelegate window property
self.window = window
// Set initial view controller from Your storyboard as root view controller of UIWindow
self.window?.rootViewController = UIStoryboard(name: "Your StoryBoard Name", bundle: nil).instantiateInitialViewController()
// Present window to screen
self.window?.makeKeyAndVisible()
}
if you are not using ScanDelegate.swift and go with AppDelegate.swift then try this way.
Without InitalViewController :
func logOut {
let storyboard = UIStoryboard(name: "Your StoryBoard", bundle: nil)
if let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController,
let loginvc = storyboard.instantiateViewController(withIdentifier: "Your Selected ViewController") as? YourSelectedViewController {
navigationController.viewControllers = [loginvc]
AppDelegate.getAppDelegate().window?.rootViewController = navigationController
}
}
With InitalViewController :
func logOut() {
let storyboard = UIStoryboard(name: "Your StoryBoard", bundle: nil)
let navigationController = storyboard.instantiateInitialViewController()
AppDelegate.getAppDelegate().window?.rootViewController = navigationController
}
Extension :
extension AppDelegate {
class func getAppDelegate() -> AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
}
来源:https://stackoverflow.com/questions/63461933/swiftui-how-to-change-rootview-controller-programmatically