首先,我的步骤是:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {我的问题是如何在ImportHome基础上显示MainHome,并单击ImportHome中的back按钮可以返回到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)
}非常感谢!
发布于 2020-07-31 04:21:54
您需要更改一些状态,以使SwiftUI视图响应并导航到ImportHome,并且需要NavigationView使导航到& back成为可能。
这里是一个方法的演示。
// 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
}https://stackoverflow.com/questions/63183249
复制相似问题