When trying to draw just like in an iOS application we faced a problem. After hours of search with no luck, we decided to ask here.
当我们试图像在iOS应用程序中那样绘制时,我们遇到了一个问题。经过几个小时的搜寻,运气不好,我们决定在这里问一下。
To make a drawing we created a class as
为了绘制一个图,我们创建了一个类。
import Cocoa
class DrawImageHolder: NSView {
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
print(" drawRect is executed ")
// drawing code
}
}
then we connected the class to a NSView
like this
然后我们将类连接到NSView
But we have received
但是我们已经收到
-[NSApplication runModalForWindow:] may not be invoked inside of transaction commit (usually this means it was invoked inside of a view's -drawRect: method.) The modal dialog has been suppressed to avoid deadlock.
-[NSApplication runModalForWindow:]不能在事务提交内部调用(通常这意味着它在视图的- drawrect:方法内部调用)。模态对话框已被抑制以避免死锁。
message from console instead of drawing.
来自控制台的消息,而不是绘图。
Besides the print(" drawRect is executed ") was not executed.
此外,打印(“drawRect被执行”)没有被执行。
Most probably we are forgetting something simple.
很可能我们忘记了一些简单的事情。
1 个解决方案
#1
1
Your class inherits from NSView:
类继承自NSView:
class DrawImageHolder: NSView
So the print
you're using:
所以你使用的打印:
print(" drawRect is executed ")
is actually NSView
's print
. The view tries to show a dialog for printing, and as the error states, it does from a place it shouldn't.
实际上是NSView打印。视图试图显示一个用于打印的对话框,当错误发生时,它会在不应该显示的地方显示。
The solution is to prefix print
with Swift
so that you're explicitly using Swift's print
instead of NSView
's print
:
解决方案是使用Swift的前缀打印,这样您就可以明确地使用Swift的打印而不是NSView的打印:
class DrawImageHolder: NSView {
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
Swift.print(" drawRect is executed ")
}
}
#1
1
Your class inherits from NSView:
类继承自NSView:
class DrawImageHolder: NSView
So the print
you're using:
所以你使用的打印:
print(" drawRect is executed ")
is actually NSView
's print
. The view tries to show a dialog for printing, and as the error states, it does from a place it shouldn't.
实际上是NSView打印。视图试图显示一个用于打印的对话框,当错误发生时,它会在不应该显示的地方显示。
The solution is to prefix print
with Swift
so that you're explicitly using Swift's print
instead of NSView
's print
:
解决方案是使用Swift的前缀打印,这样您就可以明确地使用Swift的打印而不是NSView的打印:
class DrawImageHolder: NSView {
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
Swift.print(" drawRect is executed ")
}
}