首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在SwiftUI应用程序中使用AppIntents打开特定视图

如何在SwiftUI应用程序中使用AppIntents打开特定视图
EN

Stack Overflow用户
提问于 2022-08-06 23:25:46
回答 1查看 308关注 0票数 2

我对斯威夫特的意图很陌生。使用来自WWDC 22的深入应用程序意图视频和布基示例应用程序,我已经让我的应用程序显示在快捷方式应用中,并显示一个初始的快捷方式,它打开应用程序到主视图。以下是AppIntents代码:

代码语言:javascript
运行
复制
import AppIntents

enum NavigationType: String, AppEnum, CaseDisplayRepresentable {
    case folders
    case cards
    case favorites

    // This will be displayed as the title of the menu shown when picking from the options
    static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Navigation")
    
    static var caseDisplayRepresentations: [Self:DisplayRepresentation] = [
        .folders: DisplayRepresentation(title: "Folders"),
        .cards: DisplayRepresentation(title: "Card Gallery"),
        .favorites: DisplayRepresentation(title: "Favorites")
    ]
}

struct OpenCards: AppIntent {
    
    // Title of the action in the Shortcuts app
    static var title: LocalizedStringResource = "Open Card Gallery"
    // Description of the action in the Shortcuts app
    static var description: IntentDescription = IntentDescription("This action will open the Card gallery in the Hello There app.", categoryName: "Navigation")
    // This opens the host app when the action is run
    static var openAppWhenRun = true
    
    @Parameter(title: "Navigation")
    var navigation: NavigationType

    @MainActor // <-- include if the code needs to be run on the main thread
    func perform() async throws -> some IntentResult {
                ViewModel.shared.navigateToGallery()
            return .result()
    }
    
    static var parameterSummary: some ParameterSummary {
        Summary("Open \(\.$navigation)")
    }
    
}

下面是ViewModel:

代码语言:javascript
运行
复制
import SwiftUI

class ViewModel: ObservableObject {
    
    static let shared = ViewModel()
    
    @Published var path: any View = FavoritesView()
    
    // Clears the navigation stack and returns home
    func navigateToGallery() {
        path = FavoritesView()
    }
}

现在,快捷方式允许您选择其中一个枚举(文件夹、卡片和收藏夹),但始终启动到应用程序的根目录。本质上没有什么不同,只是告诉Siri打开我的应用程序。我的应用程序在其TabView中使用ContentView和TabItems作为相关视图:

代码语言:javascript
运行
复制
            .tabItem {
                Text("Folders")
                Image(systemName: "folder.fill")
            }
            NavigationView {
                GalleryView()
            }
            .tabItem {
                Text("Cards")
                Image(systemName: "rectangle.portrait.on.rectangle.portrait.angled.fill")
            }
            NavigationView {
                FavoritesView()
            }
            .tabItem {
                Text("Favs")
                Image(systemName: "star.square.on.square.fill")
            }
            NavigationView {
                SettingsView()
            }
            .tabItem {
                Text("Settings")
                Image(systemName: "gear")
            }

如何将上面的AppIntents配置为包括“打开收藏夹视图”之类的内容,并将其启动到TabItem视图中?我认为ViewModel需要调整..。我已经尝试过将它配置为默认打开FavoritesView(),但是我迷失在正确的前进道路上。

谢谢!

编辑--用当前代码更新

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-08 20:33:14

您在正确的轨道上,您只是需要一些方式来进行编程导航。

使用TabView,您可以通过传递一个selection参数来实现这一点,该绑定是您随后可以更新以选择一个选项卡的绑定。你所有的标签在这里都能很好地工作。下面是一个示例视图:

代码语言:javascript
运行
复制
struct SelectableTabView: View {
    enum Tabs {
        case first, second
    }
    
    @State var selected = Tabs.first
    
    var body: some View {
        // selected will track the current tab:
        TabView(selection: $selected) {
            Text("First tab content")
                .tabItem {
                    Image(systemName: "1.circle.fill")
                }
                // The tag is how TabView knows which tab is which:
                .tag(Tabs.first)

            VStack {
                Text("Second tab content")
                
                Button("Select first tab") {
                    // You can change selected to navigate to a different tab:
                    selected = .first
                }
            }
            .tabItem {
                Image(systemName: "2.circle.fill")
            }
            .tag(Tabs.second)
        }
    }
}

因此,在您的代码中,ViewModel.path可以是表示可用选项卡的枚举,并且可以将绑定到路径($viewModel.path)传递到TabView。然后,只需将path = .favorites设置为导航即可。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73263899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档