我对Swift 的意图非常陌生.使用来自WWDC 22的Dive Into App Intents个视频和Booky个示例应用程序,我已经让我的应用程序显示在快捷方式应用程序中,并显示一个初始快捷方式,该快捷方式可以打开应用程序的主视图.下面是AppIntents代码:

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:

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()
    }
}

现在,快捷方式可以让你 Select 一个枚举(文件夹、卡片和Collection 夹),但总是启动到应用程序的根目录.基本上没有什么不同,只是告诉Siri打开我的应用程序.我的应用程序在其内容视图中使用TabView,相关视图使用TabItems:

            .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配置为包含类似于"Open Favorites View"的内容,并将其启动到该TabItem视图中?我认为ViewModel需要调整一下...我试图将其配置为默认打开FavoritesView(),但我在前进的正确道路上迷路了.

谢谢!

[编辑--使用当前代码更新]

推荐答案

您是在正确的轨道上,您只需要一些方法来进行程序化导航.

对于TabView,您可以通过传递selection参数来做到这一点,该参数是一个绑定,您可以更新该绑定以 Select 一个选项卡.所有选项卡的枚举在这里都能很好地工作.以下是一个示例视图:

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设置为导航.

Ios相关问答推荐

将图案UI视图动画化以模拟进度条

实例方法wait在异步上下文中不可用;请改用TaskGroup;这是Swift 6中的错误''

SwiftUI检测顶级和安全区域插图

为什么iOS委派在KMM中不能正常工作?

在 iOS 上以后台模式安排任务(屏幕时间 API)

如果使用 .onAppear 设置,为什么 SwiftUI Picker 不显示正确的选定值

Swift:判断 Foundation (NS)Errors 与自定义协议的一致性

iOS - 判断开发者模式是否开启 (Swift)

iOS应用程序崩溃,必需条件为false:isformatsamplerateandchannelcountvalid(format)

集成 IDNow SDK 时未找到 Xcode FLAnimatedImage.h 文件问题

SwiftUI DatePicker 格式在每个设备上都不一样,如何让它总是显示相同的 Select 版本?

try 在 iOS 中分发 Flutter 应用程序时出现Invalid Provisioning Profile Signature错误

如何在 Swift 中从另一个视图控制器将元素附加到数组中

Flutter:无法构建 iOS 应用程序ARCHIVE FAILED,为设备归档时遇到错误

无法同时满足约束 - 没有适当的约束

如何阻止图像在 UIImageView 中拉伸?

快速禁用 UITextfield 的用户输入

NSURLSession:如何增加 URL 请求的超时时间?

如何在 iOS 中获取正在运行的应用程序的名称

UITableView 中的圆形 UIImageView 没有性能影响?