如何为swiftUI设置初始视图控制器

如何为swiftUI设置初始视图控制器,swift,swiftui,Swift,Swiftui,我通常使用故事板通过单击属性检查器将视图控制器设置为初始视图控制器 如何在swift UI中设置初始视图控制器 系统信息:Swift 5,Xcode 11.3.1。在SceneDelegate.Swift中 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { let contentView =

我通常使用故事板通过单击属性检查器将视图控制器设置为初始视图控制器

如何在swift UI中设置初始视图控制器

系统信息:Swift 5,Xcode 11.3.1。

在SceneDelegate.Swift中

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    let contentView = ContentView().environment(\.managedObjectContext, context)

    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()
    }
}
将行
let contentView=contentView()…
更改为
YourInitialView()…

结果应该是这样的

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // change this line to your initial view controller class name
    let contentView = YourInitalView().environment(\.managedObjectContext, context)
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()
    }
}

不能将UIViewController与SwiftUI一起使用,在SwiftUI中,视图是结构

在SwiftUI中更改初始视图的步骤

  • Open SceneDelegate(非AppDelegate)
  • 升级场景(场景:UIScene,willConnectTo会话:UISceneSession,选项connectionOptions:UIScene.connectionOptions)功能如下
  • 其中SignInInputView的结构如下所示

    struct SignInInputView: View {
        @State private var userNameString: String = ""
        @State private var passwordString: String = ""
        var body: some View {
    
            HStack {
                Spacer(minLength: 20)
                VStack(alignment: .leading, spacing: 20) {
                    TextField("Enter UserName/Password", text: $userNameString, onEditingChanged: { (value) in
                        print(value)
                    }, onCommit: {
                        print(self.userNameString)
                    }).frame(height: 60)
                        .padding([.leading, .trailing], 20)
                        .background(Color.red)
    
                    TextField("Enter Password", text: $passwordString, onEditingChanged: { (value) in
                        print(value)
                    }, onCommit: {
                        print(self.passwordString)
                    }).frame(height: 60)
                        .padding([.leading, .trailing], 20)
                        .background(Color.green)
                }
                Spacer(minLength: 20)
            }
        }
    }
    

    您在SwiftUI中并没有真正使用视图控制器。也没有故事板。下面的答案显示了如何将不同的视图设置为初始视图。如果您想切换内容,例如登录/未登录,您可以使用RootView,例如在其主体中使用If语句,并有条件地加载不同的子视图。我明白了,我想在这种情况下,不可能在同一项目中同时使用swiftui和viewcontroller,或者确实存在?是的,您当然可以。谷歌快捷界面和视图控制器。你不知道我花了多长时间找到这个
    struct SignInInputView: View {
        @State private var userNameString: String = ""
        @State private var passwordString: String = ""
        var body: some View {
    
            HStack {
                Spacer(minLength: 20)
                VStack(alignment: .leading, spacing: 20) {
                    TextField("Enter UserName/Password", text: $userNameString, onEditingChanged: { (value) in
                        print(value)
                    }, onCommit: {
                        print(self.userNameString)
                    }).frame(height: 60)
                        .padding([.leading, .trailing], 20)
                        .background(Color.red)
    
                    TextField("Enter Password", text: $passwordString, onEditingChanged: { (value) in
                        print(value)
                    }, onCommit: {
                        print(self.passwordString)
                    }).frame(height: 60)
                        .padding([.leading, .trailing], 20)
                        .background(Color.green)
                }
                Spacer(minLength: 20)
            }
        }
    }