我似乎在引用我的插座时遇到了麻烦,我终于得到了拖放工作,(在 stackoverflow 成员的帮助下)但仍然无法让这个插座工作。
class controller(NSWindow):
form_file = IBOutlet()
mainWindow = IBOutlet()
#drag and drop ability
def awakeFromNib(self):
self.registerForDraggedTypes_([NSFilenamesPboardType, None])
print 'registerd drag type'
def draggingEntered_(self, sender):
print 'dragging entered'
pboard = sender.draggingPasteboard()
types = pboard.types()
opType = NSDragOperationNone
if NSFilenamesPboardType in types:
opType = NSDragOperationCopy
return opType
def performDragOperation_(self, sender):
print 'preform drag operation'
pboard = sender.draggingPasteboard()
successful = False
if NSFilenamesPboardType in pboard.types():
fileAStr = pboard.propertyListForType_(NSFilenamesPboardType)[0].encode('utf-8')
successful = True
#edit some things
print type(fileAStr)
self.form_file.setStringValue_(fileAStr)
return successful
我可以删除文件,但我无法form_file
从performDragOperation
函数内部引用插座。我想使用删除文件中的 URL 更新 NSTextField,但它返回NoneType
错误。
(reason '<type 'exceptions.TypeError'>: 'NoneType' object is not callable') was raised during a dragging session
我试图捕捉错误,所以我在我的awakeFromNib
函数中添加了以下内容
print type(self.formfile)
print type(self.mainWindow)
NSLog("formfile = %p", self.formfile)
NSLog("mainWindow = %p", self.mainWindow)
NSLog("self = %p", self)
print self
这将返回:
registerd drag type
<type 'NoneType'>
<type 'NoneType'>
2013-01-12 16:32:29.211 app [463:303] formfile = 0x10013c148
2013-01-12 16:32:29.212 app[463:303] mainWindow = 0x10013c148
2013-01-12 16:32:29.212 app[463:303] self = 0x105aba890
<controller: 0x1008a1600>
registerd drag type
<objective-c class NSTextField at 0x7fff785296e8>
<objective-c class controller at 0x10541b6a0>
2013-01-12 16:32:29.216 app[463:303] formfile = 0x105abaa10
2013-01-12 16:32:29.216 app[463:303] mainWindow = 0x105abaa10
2013-01-12 16:32:29.216 app[463:303] self = 0x105aba890
<controller: 0x1008afe00>
但是如果我在 draggingEntered 函数中进行相同的测试,那么我只会得到一个控制器和对象(那个NoneType
)。问题是它两次运行 awakeFromNib 方法对吗?我认为这可能是我的 xib 文件的问题,但我已经检查过了,似乎无法找出问题所在。
有任何想法吗,
汤姆