首先,我的步骤如下:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
我的问题是如何显示MainHome上的ImportHome基础,然后单击ImportHome中的后退按钮可以返回MainHome?
我的源代码如下:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
let store = Store.sharedInstance
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Set the environmentObject
let contentView = MainHome()
.environmentObject(store)
showRootView(scene, rootView: contentView)
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let urlContext = URLContexts.first else {
return
}
addSubView(scene, subView: ImportHome())
}
private func showRootView<T: View>(_ scene: UIScene, rootView: T) {
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: rootView)
self.window = window
window.makeKeyAndVisible()
}
}
private func addSubView<T: View>(_ scene: UIScene, subView: T) {
// Todo: How to show the sub view
let subViewUIHostingController = UIHostingController(rootView: subView)
self.window?.rootViewController?.navigationController?.pushViewController(subViewUIHostingController, animated: true)
}
非常感谢您!
答案 0 :(得分:0)
您需要更改某些状态以使SwiftUI视图做出反应并导航至ImportHome
,并且需要NavigationView
才能进行往返导航。
这是方法的演示。
// An observable class that
class ImportState: ObservableObject {
@Published var urlContext: UIOpenURLContext?
@Published var showImport = false
}
struct ImportHome: View {
@EnvironmentObject var importState: ImportState
var body: some View {
Text("Do Import Here with \(importState.urlContext?.url.absoluteString ?? "<none>")")
}
}
struct MainHome: View {
@EnvironmentObject var importState: ImportState
var body: some View {
NavigationView {
Text("MainHome Content Here")
.background(
// hidden navigation link to go to ImportHome
NavigationLink(destination: ImportHome(),
isActive: self.$importState.showImport,
label: {EmptyView()})
)
.navigationTitle("")
.navigationBarHidden(true)
}
}
}
现在将其集成到SceneDelegate
中class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// ... other code
let importState = ImportState()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Set the environmentObject
let contentView = MainHome()
.environmentObject(store)
.environmentObject(importState) // << inject
// .. other code
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let urlContext = URLContexts.first else {
return
}
importState.urlContext = urlContext // << store
importState.showImport = true // << activate navigation link
}