【发布时间】:2021-04-20 11:35:50
【问题描述】:
我正在使用它在聚光灯上添加项目,现在我可以在聚光灯上搜索这些项目
但是当我点击聚光灯上的项目时,我怎样才能进入详细信息页面?
我找不到 swiftui-app 生命周期解决方案
添加项目:
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeContact as String)
attributeSet.title = task
attributeSet.contentDescription = task
attributeSet.relatedUniqueIdentifier = uuid.uuidString
attributeSet.identifier = uuid.uuidString
attributeSet.addedDate = Date()
let searchableItem = CSSearchableItem(uniqueIdentifier: uuid.uuidString, domainIdentifier: aType, attributeSet: attributeSet)
CSSearchableIndex.default()
.indexSearchableItems([searchableItem]) { error in
if let error = error {
print("Error indexing: \(error)")
} else {
print("Indexed.")
} // error
} // .indexSearchableItems
我的 app.swift
import SwiftUI
import Intents
import CoreSpotlight
import CoreServices
let aType = "com.example.icecream-selection"
@main
struct DevoteApp: App {
let persistenceController = PersistenceController.shared
let nsUserActivity: NSUserActivity = NSUserActivity(activityType: aType)
@AppStorage("isDarkMode") var isDarkMode: Bool = false
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
// CSSearchableItemActionType
if nsUserActivity.activityType == CSSearchableItemActionType {
if (nsUserActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String) != nil {
let context = PersistenceController.shared.container.viewContext
let newItem = Item(context: context)
TodoDetail(item:newItem)
} // nil
} else {
ContentView()
// IceContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.preferredColorScheme(isDarkMode ? .dark : .light)
.onOpenURL { url in
print("Received URL: \(url)")
}
.onContinueUserActivity( CSSearchableItemActionType) { userActivity in
if let color = userActivity.persistentIdentifier {
print(">>>>>>>SUCCESS ACTIVITY<<<<<<<")
print(color)
}
}
} // CSSearchableItemActionType
} // WindowGroup
.onChange(of: scenePhase) { newScenePhase in
switch newScenePhase {
case .active:
print("App is active")
print("KEY TYPE")
print(nsUserActivity.activityType)
print(CSSearchableItemActionType)
case .inactive:
print("App is inactive")
case .background:
print("App is in background")
@unknown default:
print("Oh - interesting: I received an unexpected new value.")
} // newScenePhase
} // .onChange
} // Scene
} // App
当用户单击项目外侧时,我怎样才能获得正确的项目并转到 TodoDetail() ?
【问题讨论】:
-
你有没有想过这个问题?
-
放弃吧。 swift ui 太难学了。人生苦短,我用flutter