- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有:
Document
, 表示文本文档。EditorView
— NSTextView
, 用 Combine 包装,绑定(bind)到 Document.content<String>
. Document
是复杂 store:ObservableObject
的一部分, 所以它可以被限制为 EditorView
实例。
当我第一次创建绑定(bind)时,它按预期工作——编辑 NSTextView 更改 Document.content
中的值.
let document1 = Document(...)
let document2 = Document(...)
var editor = EditorView(doc: document1)
但是如果改变绑定(bind)到另一个文档...
editor.doc = document2
...然后updateNSView
可以看到新document2
.但是在 Coordiantor 的 textDidChange
里面仍然引用 document1
.
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else {
return
}
self.parent.doc.content = textView.string
self.selectedRanges = textView.selectedRanges
}
所以,最初,当我设置新的 bindint 时,NSTextView 将其内容更改为 document2
, 但在我键入时,协调器将更改发送到 document1
.
是真的吗,Coordiantor 保留了它自己的 parent
副本,即使父级更改(@Binding doc
已更新),它仍然引用旧的吗?
如何让 Coordinator 反射(reflect)父绑定(bind)的变化?
谢谢!
struct Document: Identifiable, Equatable {
let id: UUID = UUID()
var name: String
var content: String
}
struct EditorView: NSViewRepresentable {
@Binding var doc: Document
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeNSView(context: Context) -> CustomTextView {
let textView = CustomTextView(
text: doc.content,
isEditable: isEditable,
font: font
)
textView.delegate = context.coordinator
return textView
}
func updateNSView(_ view: CustomTextView, context: Context) {
view.text = doc.content
view.selectedRanges = context.coordinator.selectedRanges
}
}
// MARK: - Coordinator
extension EditorView {
class Coordinator: NSObject, NSTextViewDelegate {
var parent: EditorView
var selectedRanges: [NSValue] = []
init(_ parent: EditorView) {
self.parent = parent
}
func textDidBeginEditing(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else {
return
}
self.parent.doc.content = textView.string
self.parent.onEditingChanged()
}
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else {
return
}
self.parent.doc.content = textView.string
self.selectedRanges = textView.selectedRanges
}
func textDidEndEditing(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else {
return
}
self.parent.doc.content = textView.string
self.parent.onCommit()
}
}
}
// MARK: - CustomTextView
final class CustomTextView: NSView {
private var isEditable: Bool
private var font: NSFont?
weak var delegate: NSTextViewDelegate?
var text: String {
didSet {
textView.string = text
}
}
// ...
最佳答案
Is it true, that Coordiantor keeps it's own copy of parent, and even if parent changes (@Binding doc is updated), it still references to old one?
一个parent,即这里的EditorView
,是struct,所以答案是肯定的,一般是可以复制的。
How to make Coordinator reflect parent's bindings changes?
代替(或附加于)父级,将绑定(bind)显式注入(inject) Coordinator(通过构造函数)并直接使用它。
关于swift - 如何让 Swift Coordinator 反射(reflect) parent 的绑定(bind)变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62116329/
在我的项目中,我使用 LocationSearchTable/MKLocalSearch。当用户点击一个项目时,我的添加注释方法在 MapViewController 中被调用: func dropP
我很想知道如何检查字符串是否具有两个坐标的格式,例如: (signed int x,signed int y) 我已经通过搜索找到了一些答案,但我还没有完全理解它们(刚开始使用 C++),我要求一个简
在我正在编写的程序中,我为两个类(坐标和图形)编写了代码,其中一个将另一个作为构造函数参数。当我尝试编译它时,出现以下 Graph.cpp 错误: undefined symbol : “Graph:
我在 OpenGL ES 中绘制变化的条,它有坐标 (x,y,z)。 我想在 Action_Down 上添加一些更改(例如:栏中的颜色)。有 getX(),但此方法返回以像素为单位的 x 坐标,而不是
因为我之前的问题很不清楚,所以我编辑了一下: 我有以下问题: 我想为半径为 r+fcr_size 的空心球体构建一个图案。空心球体的空腔半径应为 r。有了这个图案,我可以在许多不同的球体中心使用它,并
我想使用 Vuforia 检测标记并在其上放置一个 3d 对象。从那时起,我想在我的应用程序中使用 ARKit。我如何知道检测到的标记或 3d 对象的 ARKit 世界变换? 我正在使用相同的 Vie
我有一组纬度/经度坐标,我可以使用它们进行投影,例如 Mollweide 投影。 library(mapproj) set.seed(0) n 180] = my.points$x[my.point
我正在使用 JavaScript 进行视频处理,我做得很好,但我使用的是一种名为 canvasCtx.rect () 的方法,它接收这些参数。 然后我可以从使用 getImageData () 方法绘
我创建了一个新项目作为单 View 应用程序。在 View Controller 中添加了用于将 View 坐标转换为窗口坐标的代码: - (void) dumpFrame { CGRect
简短版本:如何将 SVG 路径添加到 Leaflet map ,以便在 map 坐标更改时(例如缩放更改或滑动时)路径会更新? 长版:你好,我有一个地形image包含建筑轮廓。对图像进行地理校正后,我
我编写了一个代码,使用 astropy 将坐标从地球固定系统转换为惯性坐标系: from astropy import coordinates as coord from astropy import
我的多显示器设置中遇到以下情况: 在此示例中,我希望将窗口精确定位在黄色箭头所示的坐标处。然而,我所拥有的只是 NSView 的坐标,它是跨越整个(更大、更上面)辅助监视器的 NSWindow 的 c
我不知道如何将 Pane 上圆形对象的所有 x 和 y 与鼠标的 x 和 y 进行比较。我正在处理的问题要求我设置它,以便鼠标的二次单击会在放置在其上时删除一个点,我想我可以通过比较圆坐标和鼠标坐标的
我正在尝试将图像中的几个点转换为 OpenCV 中的极坐标。我遇到了名为 cartToPolar 的函数,它会为我的点提供相对于 0,0 作为我的原点的极坐标。但是,我想通过将图像中的另一个点作为原点
是的,我想知道如何检查某个坐标是否在另一个坐标半径内。但这可能是一个很小的差异,因为纬度、经度、半径存储在数据库中,而我们要检查的只是给定的坐标。 示例 数据库表 name | lat
我正在使用带有图层的 map (来自示例): var lonLat = new OpenLayers.LonLat(40.4088576, -86.8576718) .
我在坐标转换方面遇到了一些麻烦。 我在屏幕上有一个已知坐标(x,y)的对象,我想将其转换为世界坐标(x,y,z),因为它会投影在相机的近平面上。 到目前为止,我可以像这样在Z平面上进行投影: var
我有4个足球场点(角点): P1(lat, lon, alt) , P2(lat, lon, alt) , P3(lat, lon, alt) , P4(lat, lon, alt) . 以及球场上的
我有一个交换了纬度和经度位置的 NetCDF 文件。 我通常使用的 Netcdf 的标准方式定义如下: Coordinates: * time (time) datetime64[n
如果 line1 和 lin2 都由 x,y,alpha 定义,其中 x,y 是直线上一点的坐标,alpha 是直线与 x=const 之间的角度,如何找到 line1 和 lin2 相交的点? 我尝
我是一名优秀的程序员,十分优秀!