简体   繁体   中英

@IBAction fails to work in Swift, though I can connect it in Interface Builder, and it works in Objective-C

In all cases, I can wire user-interface buttons to actions with Interface Builder. But the buttons work for Objective-C but not for Swift.

Objective-C example (it works):

- (IBAction)TogglePlaying:(id)sender {
(details snipped for brevity)
}

Swift example (it doesn't work, though it's wired to its button):

    @IBAction func Go(_ sender: Any) {
        print("Going")
        OutputText!.stringValue = InputText!.stringValue
    }

I have no idea of what the difference might be, because everything I've found on using IBAction in Swift indicates that I've written it correctly. Also, in Interface Builder, I've set File's Owner's Custom Class correctly.

Update:

Using ios - Find what Action is called by a Button in interface builder in debug mode - Stack Overflow Find what Action is called by a Button in interface builder in debug mode

I used "Debug View Hierarchy", right-clicked on "NSButton - Go," in the widget-hierarchy view, and selected "Print Description of NSButton - Go!"

I got
Printing description of $13:
<NSButton: 0x7fac1b116250>

I then did:
po [0x7fac1b116250 allTargets]
error: Execution was interrupted, reason: Attempted to dereference an invalid ObjC Object or send it an unrecognized selector.
The process has been returned to the state before expression evaluation.

Update:

I tried
po [0x7faf38011790 target]
(new address of that button) and I got
nil

Update:

The complete code of TLWindow, in 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
    }
}

I don't know how to show that the xib is wired up correctly without doing a lot of screenshots. But it is, with the "Go:" button connected to "Go." in "File's Owner", Also, "File's Owner" is set to "TLWindow". this class.

You are creating an instance of TLWindow in newDocument() , but then you're letting it go out-of-scope...

Try this:

@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)
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM