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

时间:2020-02-25 00:21:18

标签: swift swiftui

我通常使用情节提要通过单击属性检查器来将视图控制器设置为初始视图控制器。

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

系统信息:Swift 5,Xcode 11.3.1。

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:-2)

您不能将UIViewController与SwiftUI一起使用,在SwiftUI中,视图是Struct。

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

  1. 打开SceneDelegate(不是AppDelegate)
  2. 更新场景(_场景:UIScene,willConnectTo会话:UISceneSession,选项connectionOptions:UIScene.ConnectionOptions)功能如下所示
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        let contentView = SignInInputView()

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

SignInInputView是如下所示的Stuct

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