提问人:arroyot24 提问时间:9/6/2023 更新时间:9/6/2023 访问量:52
从文本视图内容中获取字符串
Get String from Text view content
问:
在我的一个泛型视图中,我将一个“Content”参数定义为要传递的 View,因为我需要在此泛型视图上显示 Core Data 中 FetchRequest 的行内容。对于该 ForEach 的滑动操作,我需要从该视图传递字符串:
struct MyView<Object: NSManagedObject, Content: View>: View {
var items: FetchedResults<Object>
var rowContent: (Object) -> Content
init(items: FetchedResults<Object>, rowContent: @escaping (Object) -> Content) {
self.items = items
self.rowContent = rowContent
}
@State private var editingItemName = ""
var body: some View {
ForEach(items, id: \.self) { item in
rowContent(item)
.swipeActions(edge: .leading) {
Button {
editingItemName = rowContent(item)
//Here I can't get just string value. Just something like "Text(storage: SwiftUI.Text.Storage.verbatim("text_I_need_to_get"), modifiers: [])"
这是从另一个视图调用的:
struct ParentView: View {
@FetchRequest(sortDescriptors: []) private var items: FetchedResults<MyEntity>
var body: some View {
Form {
NavigationLink(destination: MyView(items: instruments, rowContent: { item in
Text(item.name ?? "")
})) {
Label("Items", systemImage: "star")
}
//More code here
forEach 中的 rowContent(item) 工作得很好,但是我怎么能只抓取字符串“text_I_need_to_get”呢?
答: 暂无答案
评论
item
Button
editingItemName = item
String
@ObservedObject
item