我对斯威夫特的意图很陌生。使用来自WWDC 22的深入应用程序意图视频和布基示例应用程序,我已经让我的应用程序显示在快捷方式应用中,并显示一个初始的快捷方式,它打开应用程序到主视图。以下是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()
}
}
现在,快捷方式允许您选择其中一个枚举(文件夹、卡片和收藏夹),但始终启动到应用程序的根目录。本质上没有什么不同,只是告诉Siri打开我的应用程序。我的应用程序在其TabView
中使用ContentView和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配置为包括“打开收藏夹视图”之类的内容,并将其启动到TabItem视图中?我认为ViewModel
需要调整..。我已经尝试过将它配置为默认打开FavoritesView()
,但是我迷失在正确的前进道路上。
谢谢!
编辑--用当前代码更新
发布于 2022-08-08 20:33:14
您在正确的轨道上,您只是需要一些方式来进行编程导航。
使用TabView
,您可以通过传递一个selection
参数来实现这一点,该绑定是您随后可以更新以选择一个选项卡的绑定。你所有的标签在这里都能很好地工作。下面是一个示例视图:
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
设置为导航即可。
https://stackoverflow.com/questions/73263899
复制相似问题