SwiftUI:如何以编程方式更改Rootview控制器 [英] SwiftUI: How to change rootview controller programmatically

查看:65
本文介绍了SwiftUI:如何以编程方式更改Rootview控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户注销后将rootview控制器更改为其他屏幕.

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()
    }

这是我到目前为止所拥有的.它导航到其他视图,但是使新的新视图上的按钮不可用.有没有更好的方法来更改rootview或我缺少什么?

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?

推荐答案

我的方法是创建一个可观察对象,以协调各种应用程序视图之间的导航.下面是一个简化的示例,可为您提供一个想法:

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)
            }
        }
    }
}

请注意,您可以将 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()
}

要在 MainAppView LoginView 之间切换,您只需修改environmentObject showingView

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

这篇关于SwiftUI:如何以编程方式更改Rootview控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆