如何使用具有焦点的按钮启动OSX用户界面?

时间:2016-08-15 14:22:18

标签: xcode macos interface-builder

我有一个间歇性的问题让我发疯。我在Xcode中有一个OSX应用程序,它有一个简单的用户界面,带有一个按钮和一些文本字段。有时它首先关注按钮,这是我正在寻找的行为,因为我将按钮映射到IB中的回车键。有时它会在没有按下按钮的情况下开始,然后回车键不会按下按钮。我尝试使用self.buttonName.becomeFirstResponder()将按钮设置为init()中的第一响应者,但这似乎对应用程序何时突出显示并准备好按钮有很大影响是否接受回车。我觉得自己像个白痴,因为我已经在这个看似简单的问题上工作了一个多星期了。有没有人有任何想法?

@Meriw建议我将第一个响应者调用放入viewWillAppear。我有AppDelegate.swift:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var mainWindowController: MainWindowController?

    func applicationDidFinishLaunching( aNotification: NSNotification ) {

        // Create a window controller
        let mainWindowController = MainWindowController()

        // Put the window of the window controller on screen
        mainWindowController.showWindow( self )

        // Set the property to point to the window controller
        self.mainWindowController = mainWindowController
    }

    func applicationWillTerminate( aNotification: NSNotification ) {
        // Insert code here to tear down your application
    }

    // Make the program disappear after the window is closed
    func applicationShouldTerminateAfterLastWindowClosed( sender: NSApplication ) -> Bool {
        return true
    }

}

在MainWindowController.swift中:

import Cocoa

class MainWindowController: NSWindowController {

    init() {
        super.init( window: nil )

        // load the contents of the nib, and set the owner as self, which connects the oultlets
        NSBundle.mainBundle().loadNibNamed( windowNibName, owner: self, topLevelObjects: nil )
    }

    required init?( coder: NSCoder ) {
        super.init( coder: coder )
    }

    override var windowNibName: String {
        return "MainWindowController"
    }

...

viewWillAppear将在何处实施以及如何实现?

非常感谢提前!

0 个答案:

没有答案