[英]Does NSWindowController showWindow Trigger Embedded NSView setNeedsDisplay?
[英]call a func in NSWindowController from custom class for an NSView
在OS X的Swift中,我有一个“工作表”作为NSWindowController
。我在工作表上放置了一个NSView
,在名为CustomView
的类中有一个自定义控制器。 我在工作表上和在自定义视图控制器中所做的所有工作都很好。
class MySheet: NSWindowController {
//...
}
class CustomView: NSView {
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect) {
// Draw some things...
}
}
override func mouseDown(theEvent: NSEvent) {
// Change text on a label (or whatever) in MySheet()
}
}
现在,我需要响应CustomView
用户输入,特别是,在视图上单击鼠标,然后在MySheet()中修改标签(或其他控件)。 这必须非常简单,但是我不知道该怎么做。 我最大的绊脚石是我没有MySheet()实例; 它隐藏在系统的工作方式中。 我可以在MySheet()中回调静态函数,但这不能让我通过@IBOutlet更改标签或其他控件。
我知道上述问题很可能是一个幼稚的问题,但希望能对解决方案有所帮助。
当然(!),应该使用委托来解决此问题,并且在正确设置时,它可以正常工作。 所以我有这样的:
protocol ClickableCurves {
func clickedOnCurve(index: Int)
}
class MySheet: NSWindowController, ClickableCurves {
private let lineColorCount = 16
func clickedOnCurve(index: Int) {
Swift.print("came back as \(index)")
}
override func windowDidLoad() {
super.windowDidLoad()
(curveView as! DefaultCurveAttrView).delegate = self
}
@IBOutlet weak var curveView: NSView!
}
class DefaultCurveAttrView: NSView {
var delegate: ClickableCurves? = nil
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
}
override func mouseDown(theEvent: NSEvent) {
let frameRelWindow = self.convertRect(bounds, toView: nil)
let x = theEvent.locationInWindow.x - frameRelWindow.origin.x
let y = theEvent.locationInWindow.y - frameRelWindow.origin.y
let xm = bounds.width / 10
let dy = bounds.height / CGFloat(lineColorCount + 1)
var yc: CGFloat = dy / 2
if x >= xm && x <= 9 * xm {
for i in 1 ... lineColorCount {
yc += dy
if yc > y || i == lineColorCount {
if delegate != nil {
delegate!.clickedOnCurve(i - 1)
}
break
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.