Core Data 结果进入 SwiftUI Picker 视图,无需预选选项
Core Data results into a SwiftUI Picker view without preselecting an option
我正在从 Core Data 获取数据并将其显示在 Picker 中,此视图是在 SwiftUI 中构建的。起初我努力让 selection 从选择器中工作,但发现如果我预先 select 来自我的获取结果的第一个对象,在视图的初始化中,选择器 selection有效。
我想要的是能够将我的数据加载到选择器中,而不是select 对象之一。同样,如果我更新下面的代码,所以没有预selected,那么我就不能select任何东西。我进入了选项屏幕,但是当我点击其中一个时,没有任何反应,我只是在行上看到那个小闪光灯。我必须点击“后退”按钮才能返回到表单视图。
关于如何在不预先select选择其中一个选项的情况下将核心数据加载到选择器中的任何想法?
-谢谢!
import SwiftUI
import CoreData
struct RecordCreateview: View {
@FetchRequest private var actions: FetchedResults<Actions>
@State private var selectedAction: Actions
init(context: NSManagedObjectContext) {
let fetchRequest: NSFetchRequest<Actions> = Actions.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Actions.action, ascending: true)]
fetchRequest.predicate = NSPredicate(value: true)
self._actions = FetchRequest(fetchRequest: fetchRequest)
do {
let fetchResult = try context.fetch(fetchRequest)
self._selectedAction = State(initialValue: fetchResult[0])
} catch {
fatalError("Problem fetching Action records.")
}
}
var body: some View {
NavigationView {
Form {
Picker("Select action", selection: $selectedAction){
ForEach(actions) { action in
if action.title == true {
Text("\(action.action!)").tag(action)
}
}
}
}
}
}
}
您可以将 selectedAction 设置为 init(...)
中不存在的 Actions
,如下所示:
self._selectedAction = State(initialValue: Actions(context: context))
不会在选取器中设置预选对象。
struct RecordCreateview: View {
@FetchRequest private var actions: FetchedResults<Actions>
@State private var selectedAction: Actions
init(context: NSManagedObjectContext) {
let fetchRequest: NSFetchRequest<Actions> = Actions.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Actions.action, ascending: true)]
fetchRequest.predicate = NSPredicate(value: true)
self._actions = FetchRequest(fetchRequest: fetchRequest)
self._selectedAction = State(initialValue: Actions(context: context)) // <--- here
}
var body: some View {
NavigationView {
Form {
Picker("Select action", selection: $selectedAction){
ForEach(actions) { action in
if action.title == true {
Text("\(action.action!)").tag(action)
}
}
}
}
}
}
}
我正在从 Core Data 获取数据并将其显示在 Picker 中,此视图是在 SwiftUI 中构建的。起初我努力让 selection 从选择器中工作,但发现如果我预先 select 来自我的获取结果的第一个对象,在视图的初始化中,选择器 selection有效。
我想要的是能够将我的数据加载到选择器中,而不是select 对象之一。同样,如果我更新下面的代码,所以没有预selected,那么我就不能select任何东西。我进入了选项屏幕,但是当我点击其中一个时,没有任何反应,我只是在行上看到那个小闪光灯。我必须点击“后退”按钮才能返回到表单视图。
关于如何在不预先select选择其中一个选项的情况下将核心数据加载到选择器中的任何想法?
-谢谢!
import SwiftUI
import CoreData
struct RecordCreateview: View {
@FetchRequest private var actions: FetchedResults<Actions>
@State private var selectedAction: Actions
init(context: NSManagedObjectContext) {
let fetchRequest: NSFetchRequest<Actions> = Actions.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Actions.action, ascending: true)]
fetchRequest.predicate = NSPredicate(value: true)
self._actions = FetchRequest(fetchRequest: fetchRequest)
do {
let fetchResult = try context.fetch(fetchRequest)
self._selectedAction = State(initialValue: fetchResult[0])
} catch {
fatalError("Problem fetching Action records.")
}
}
var body: some View {
NavigationView {
Form {
Picker("Select action", selection: $selectedAction){
ForEach(actions) { action in
if action.title == true {
Text("\(action.action!)").tag(action)
}
}
}
}
}
}
}
您可以将 selectedAction 设置为 init(...)
中不存在的 Actions
,如下所示:
self._selectedAction = State(initialValue: Actions(context: context))
不会在选取器中设置预选对象。
struct RecordCreateview: View {
@FetchRequest private var actions: FetchedResults<Actions>
@State private var selectedAction: Actions
init(context: NSManagedObjectContext) {
let fetchRequest: NSFetchRequest<Actions> = Actions.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Actions.action, ascending: true)]
fetchRequest.predicate = NSPredicate(value: true)
self._actions = FetchRequest(fetchRequest: fetchRequest)
self._selectedAction = State(initialValue: Actions(context: context)) // <--- here
}
var body: some View {
NavigationView {
Form {
Picker("Select action", selection: $selectedAction){
ForEach(actions) { action in
if action.title == true {
Text("\(action.action!)").tag(action)
}
}
}
}
}
}
}