![](/img/trans.png)
[英]How can I specify protocol same name to interface defined in Objective-C from Swift
[英]@IBAction fails to work in Swift, though I can connect it in Interface Builder, and it works in Objective-C
在所有情况下,我都可以使用 Interface Builder 将用户界面按钮连接到操作。 但是按钮适用于 Objective-C 但不适用于 Swift。
Objective-C 示例(有效):
- (IBAction)TogglePlaying:(id)sender {
(details snipped for brevity)
}
Swift 示例(它不起作用,尽管它连接到它的按钮):
@IBAction func Go(_ sender: Any) {
print("Going")
OutputText!.stringValue = InputText!.stringValue
}
我不知道可能有什么区别,因为我在 Swift 中使用 IBAction 时发现的所有内容都表明我已经正确编写了它。 此外,在 Interface Builder 中,我已正确设置文件所有者的自定义 Class。
更新:
使用 ios - 在调试模式下查找界面构建器中的按钮调用的操作 - 堆栈内存溢出 在调试模式下查找界面构建器中的按钮调用的操作
我使用了“Debug View Hierarchy”,在 widget-hierarchy 视图中右键单击“NSButton - Go”,然后选择“Print Description of NSButton - Go!”
我有
$13 打印说明:
<NSButton: 0x7fac1b116250>
然后我做了:
po [0x7fac1b116250 allTargets]
错误:执行被中断,原因:试图取消引用无效的 ObjC Object 或向其发送无法识别的选择器。
该过程已返回到表达式评估之前的 state。
更新:
我试过了
po [0x7faf38011790 目标]
(那个按钮的新地址)我得到了
零
更新:
TLWindow的完整代码,在Swift:
import Cocoa
class TLWindow: NSWindowController {
@IBOutlet weak var InputText: NSTextField!
@IBOutlet weak var OutputText: NSTextField!
override var windowNibName: NSNib.Name? {
return NSNib.Name("TLWindow")
}
@IBAction func Go(_ sender: Any?) {
print("Going")
OutputText!.stringValue = InputText!.stringValue
}
}
我不知道如何在不做大量屏幕截图的情况下证明 xib 已正确连接。 但它是,“开始:”按钮连接到“开始”。 在“文件的所有者”中,“文件的所有者”也设置为“TLWindow”。 这节课。
您正在newDocument()
中创建TLWindow
的实例,但随后您让它 go 超出范围...
尝试这个:
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
// add a property to "hang onto" the instance
var myTLWindow: TLWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
newDocument(self)
}
// Create an app window
@IBAction func newDocument(_ sender: Any?) {
let wc = TLWindow()
// add this line
myTLWindow = wc
wc.showWindow(self)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.