我如何在孩子们的视图中启动Swiftui应用程序?

发布于 2025-02-13 22:34:06 字数 1062 浏览 0 评论 0原文

好吧,这就是我的应用程序的样子。

@main
struct MemorizableApp: App {
    init() {
        FirebaseApp.configure()                             // Configure the FirebaseApp instance
        Database.database().isPersistenceEnabled = true     // Set DB persistence to true
    }
    
    var body: some Scene {
        WindowGroup {
            if Auth.auth().currentUser == nil {
                SignInView()
                    .onOpenURL{ url in
                        GIDSignIn.sharedInstance.handle(url)
                    }
            } else {
                MainView()
            }
        }
    }
}

这是MemorizableApp.swift。如您所见,该应用在MainView中启动。但是我希望该应用程序可以在笔记本视图中启动。显然,在上面的代码中将Mainview更改为NotebookSview不起作用,因为1)NotebookSview中没有NavigationView,而2)我将无法导航回MainView。但是,某种程度上,Apple的快捷方式应用程序完全可以做到这一点,而无需创建自定义的返回按钮。我该如何实现这种行为?

Okay, here's what my app looks like.
View Hierachy

@main
struct MemorizableApp: App {
    init() {
        FirebaseApp.configure()                             // Configure the FirebaseApp instance
        Database.database().isPersistenceEnabled = true     // Set DB persistence to true
    }
    
    var body: some Scene {
        WindowGroup {
            if Auth.auth().currentUser == nil {
                SignInView()
                    .onOpenURL{ url in
                        GIDSignIn.sharedInstance.handle(url)
                    }
            } else {
                MainView()
            }
        }
    }
}

And this is MemorizableApp.swift. As you can see, the app starts in MainView. But I want the app to launch in NotebooksView instead. Obviously, changing MainView to NotebooksView in the code above won't work because 1) NotebooksView doesn't have a NavigationView in it, and 2) I won't be able to navigate back to MainView. But somehow Apple's Shortcuts app did exactly this without creating a custom back button. How can I achieve this behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国际总奸 2025-02-20 22:34:06

您需要做的是以编程方式激活navigationLink

in mainView add:@State var showerNavigate = false,然后修改navigationlink到以下内容:

NavigationLink(destination: NoteBooksView(), isActive: $shouldNavigate) {
    ...
}

MemorizableApp

@main
struct MemorizableApp: App {
    init() {
        FirebaseApp.configure()                             // Configure the FirebaseApp instance
        Database.database().isPersistenceEnabled = true     // Set DB persistence to true
    }
    var body: some Scene {
        WindowGroup {
            if Auth.auth().currentUser == nil {
                SignInView()
                    .onOpenURL{ url in
                        GIDSignIn.sharedInstance.handle(url)
                    }
            } else {
                MainView(shouldNavigate: true) //set it to true
            }
        }
    }
}

注意:navigationLink的使用(目标:content:content,isActive:iSACTIVE:绑定< bool>)& navigationView ios 16中的debected 。看看在这里& 在这里

What you need to do is activate your NavigationLink programmatically:

In MainView add: @State var shouldNavigate = false, then modify your NavigationLink to the following:

NavigationLink(destination: NoteBooksView(), isActive: $shouldNavigate) {
    ...
}

And in MemorizableApp:

@main
struct MemorizableApp: App {
    init() {
        FirebaseApp.configure()                             // Configure the FirebaseApp instance
        Database.database().isPersistenceEnabled = true     // Set DB persistence to true
    }
    var body: some Scene {
        WindowGroup {
            if Auth.auth().currentUser == nil {
                SignInView()
                    .onOpenURL{ url in
                        GIDSignIn.sharedInstance.handle(url)
                    }
            } else {
                MainView(shouldNavigate: true) //set it to true
            }
        }
    }
}

Note: The use of NavigationLink(destination: Content, isActive: Binding<Bool>) & NavigationView is deprecated in iOS 16. Take a look here & here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文