Xcode 12 在 SwiftUI 项目中使用 ViewRouter
Use ViewRouter in SwiftUI Project in Xcode 12
我尝试在一个新的 Xcode 12 项目中使用 this 方法(见附件 link)作为为 SwiftUI 应用程序创建登录页面的方法,但我有不知道要将什么添加到主 App 结构的问题。我仍然是初学者,并尝试将 ContentView().environmentObject(ViewRouter())
添加到主应用程序结构中的 WindowGroup。我是不是完全错了,或者为什么 Xcode 不构建视图?有人可以帮忙吗?
在工作代码片段下方:
import SwiftUI
import Foundation
import Combine
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: MotherView().environmentObject(ViewRouter()))
self.window = window
window.makeKeyAndVisible()
}
}
. . .
}
class ViewRouter: ObservableObject {
let objectWillChange = PassthroughSubject<ViewRouter,Never>()
var currentPage: String = "page1" {
didSet {
withAnimation() {
objectWillChange.send(self)
}
}
}
}
struct MotherView : View {
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
VStack {
if viewRouter.currentPage == "page1" {
ContentViewA()
} else if viewRouter.currentPage == "page2" {
ContentViewB()
.transition(.scale)
}
}
}
}
struct ContentViewA : View {
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
Button(action: {self.viewRouter.currentPage = "page2"}) {
Text("Login")
}
}
}
struct ContentViewB : View {
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
Button(action: {self.viewRouter.currentPage = "page1"}) {
Text("Logout")
}
}
}
现在我想用 Xcode 12 样式替换 SceneDelegate,但以下不起作用。知道为什么吗?
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
MotherView().environmentObject(ViewRouter())
}
}
}
请尝试下一步:
删除整个 SceneDelegate class(在你的情况下不需要 SceneDelegate class)
修改您的 ViewRouter class 如:
class ViewRouter: ObservableObject {
@Published var currentPage: String = "page1"
}
我尝试在一个新的 Xcode 12 项目中使用 this 方法(见附件 link)作为为 SwiftUI 应用程序创建登录页面的方法,但我有不知道要将什么添加到主 App 结构的问题。我仍然是初学者,并尝试将 ContentView().environmentObject(ViewRouter())
添加到主应用程序结构中的 WindowGroup。我是不是完全错了,或者为什么 Xcode 不构建视图?有人可以帮忙吗?
在工作代码片段下方:
import SwiftUI
import Foundation
import Combine
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: MotherView().environmentObject(ViewRouter()))
self.window = window
window.makeKeyAndVisible()
}
}
. . .
}
class ViewRouter: ObservableObject {
let objectWillChange = PassthroughSubject<ViewRouter,Never>()
var currentPage: String = "page1" {
didSet {
withAnimation() {
objectWillChange.send(self)
}
}
}
}
struct MotherView : View {
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
VStack {
if viewRouter.currentPage == "page1" {
ContentViewA()
} else if viewRouter.currentPage == "page2" {
ContentViewB()
.transition(.scale)
}
}
}
}
struct ContentViewA : View {
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
Button(action: {self.viewRouter.currentPage = "page2"}) {
Text("Login")
}
}
}
struct ContentViewB : View {
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
Button(action: {self.viewRouter.currentPage = "page1"}) {
Text("Logout")
}
}
}
现在我想用 Xcode 12 样式替换 SceneDelegate,但以下不起作用。知道为什么吗?
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
MotherView().environmentObject(ViewRouter())
}
}
}
请尝试下一步:
删除整个 SceneDelegate class(在你的情况下不需要 SceneDelegate class)
修改您的 ViewRouter class 如:
class ViewRouter: ObservableObject { @Published var currentPage: String = "page1" }