问题
First, my step is just follows:
- Open my application(called appA), and it show the MainHome View
- Back to the launcher, and call another application(called appB), and click the export file to the appA
- And now it would call the function:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
My problem is how could I show the ImportHome base on the MainHome and click the back button in the ImportHome could back to MainHome?
My source code is just as follow:
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)
}
Thank you very much!
回答1:
You need to change some state to make SwiftUI view to react and navigate to ImportHome
and you need NavigationView
to make navigation to & back possible.
Here is a demo of approach.
// 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)
}
}
}
and now integrate above into 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/swiftui-how-could-i-show-the-new-view-base-on-the-current-scene