2

これは簡単な質問かもしれませんが、私は Swift を初めて使用するので、どの用語を検索すればよいかわかりません。

doSomethingメニュー バー アプリケーションの場合、いくつかのボタンにバインドされている場合に問題なく動作する、というカスタム関数を実装しました。

Class MainViewController:
{
    @IBAction func doSomething(sender: NSButton) 
    {
        // Do something when NSButton is pressed
    }
}

ただし、ボタンの左クリックと右クリックを区別する必要があります。私の場合はNSStatusBarButton. この回答からの提案に従って、私は以下を my に書きましたAppDelegate.swift:

@NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {

    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)
    var mainViewController: MainViewController?


    func applicationDidFinishLaunching(notification: NSNotification) 
    {
        if let button = statusItem.button 
        {
            button.action = #selector(customClickAction)
            button.sendActionOn(Int(NSEventMask.RightMouseDownMask.rawValue | NSEventMask.LeftMouseDownMask.rawValue))
        }
    }


    func customClickAction(sender: NSButton) 
    {
        let event:NSEvent! = NSApp.currentEvent!

        if (event.type == NSEventType.RightMouseDown) 
        {
            print("Right mouse button down")
        } 
        else if (event.type == NSEventType.LeftMouseDown)
        {
            print("Left mouse button down")
            mainViewController?.doSomething(_:) // THIS DOES NOT WORK
         }
    }
}

上記のコード スニペットを使用すると、XCode で「式が未使用の関数に解決されます」というエラー メッセージが表示されます。関数内のクラスdoSomethingから関数を適切に呼び出す方法、または同等に、 ビアのアクションを にリダイレクトする方法がわかりません。この質問が Swift の専門家にとって些細すぎるように思えるかもしれないことをお詫びしますが、私はこれを理解しようとして本当に絶望しています。MainViewControllercustomClickActionstatusItem.buttoncustomClickActiondoSomething

編集:

関数が存在しない場合は、関数を呼び出すようにcustomClickAction書き込むだけbutton.action = #selector(mainViewController?.show(_:))で、すべてが機能します。applicationDidFinishLaunchingただし、私の問題の一部は、カスタム関数で同じことを行うと、マウスの左ボタンが初めて押されたときにバインディングが上書きされることです。

4

1 に答える 1

2

同じ悩みを持った方からの質問Expression resolves to an unused functionです。彼/彼女の場合、問題は関数が関数の()後なしで呼び出されたことstopでしstop()た。

さて、コンパイラが何を伝えようとしているのかがわかったので、それを解決するために何をすべきかを考えてみることができます。

あなたの場合、問題は、メソッドにsenderパラメーターを送信する必要があることです。そのパラメーターは、自分自身を宣言したとおりdoSomethingの型でなければなりません。NSButton

@IBAction func doSomething(sender: NSButton) 
{
    // Do something when NSButton is pressed
}

したがって、あなたの場合、senderパラメーターとして取得した を次のように渡すだけの場合customClickAction:

func customClickAction(sender: NSButton)
{
    let event:NSEvent! = NSApp.currentEvent!

    if (event.type == NSEventType.RightMouseDown)
    {
        print("Right mouse button down")
    }
    else if (event.type == NSEventType.LeftMouseDown)
    {
        print("Left mouse button down")
        mainViewController?.doSomething(sender)
    }
}

その後、コンパイラは満足しているようです。

全体はこちらAppDelegate

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)
    var mainViewController: MainViewController? = MainViewController() //initialized


    func applicationDidFinishLaunching(notification: NSNotification)
    {
        if let button = statusItem.button
        {
            button.action = #selector(customClickAction)
            button.sendActionOn(Int(NSEventMask.RightMouseDownMask.rawValue | NSEventMask.LeftMouseDownMask.rawValue))
        }
    }


    func customClickAction(sender: NSButton)
    {
        let event:NSEvent! = NSApp.currentEvent!

        if (event.type == NSEventType.RightMouseDown)
        {
            print("Right mouse button down")
        }
        else if (event.type == NSEventType.LeftMouseDown)
        {
            print("Left mouse button down")
            mainViewController?.doSomething(sender)
        }
    }
}

お役に立てば幸いです。

編集のフォローアップ

あなたが書く

関数 customClickAction が存在しない場合は、単に button.action = #selector(mainViewController?.show(_:)) を applicationDidFinishLaunching に記述して関数を呼び出すだけで、すべてが機能します。

doSomethingここですべてを誤解している可能性がありますが、メソッドを「ただ」割り当ててからbutton.action、左ボタンまたは右ボタンのチェックをそのメソッドに移動してみませんか?

だからあなたは言うでしょう:

button.action = #selector(mainViewController?.doSomething(_:))

あなたdoSomethingは次のようになります:

    @IBAction func doSomething(sender: NSButton)
{
    // Do something when NSButton is pressed
    let event:NSEvent! = NSApp.currentEvent!

    if (event.type == NSEventType.RightMouseDown)
    {
        print("Right mouse button down")
    }
    else if (event.type == NSEventType.LeftMouseDown)
    {
        print("Left mouse button down")
    }
}

そうすると、「マウスの左ボタンを押した」と「マウスの右ボタンを押した」がコンソールに表示されます。

スイフト3アップデート

@ixany がコメントに書いているように:

それは私ですか、それともSwift 3ですか?:)現在のXcode Betaを使用して適応させることができないため、ソリューションをSwift 3に更新する必要があるようです

構文を Swift 3.0 にアップグレードしようとしましたが、いくつか問題があるようです。

最初の問題は、 を割り当てるときに のdoSomethingメソッドに直接行くことができなかったため、いわば「ローカル」ステップを追加したことです。MainViewControllerselector

現在AppDelegateは次のようになっています。

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    let statusItem = NSStatusBar.system().statusItem(withLength: -2)
    var mainViewController: MainViewController? = MainViewController()

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        if let button = statusItem.button
        {
            button.action = #selector(AppDelegate.localButtonAction(sender:))
            button.sendAction(on: [NSEventMask.leftMouseDown, NSEventMask.rightMouseDown])
        }
    }

    func localButtonAction(sender: NSStatusBarButton) {
        mainViewController?.doSomething(sender: sender)
    }
}

(「localButtonAction」メソッドに注意してください。他の誰かがこれを行うためのより良い方法を持っていることを願っています)

そして、MainViewController次のようになります。

import Cocoa

class MainViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }

    @IBAction func doSomething(sender: NSStatusBarButton) {
        // Do something when NSButton is pressed
        let event:NSEvent! = NSApp.currentEvent!
        if (event.type == NSEventType.rightMouseDown)
        {
            print("Right mouse button down")
        }
        else if (event.type == NSEventType.leftMouseDown)
        {
            print("Left mouse button down")
        }
    }    
}

これに関する問題は、私が見る限り、event決してタイプではないということです。rightMouseDown私はおそらく何か間違ったことをしており、他の誰かがこの作業を行うことができることを願っていますが、少なくともコンパイルされ、Swift 3.0 構文です (Xcode 8.0 - ベータ 6 を使用:))

@ixanyのお役に立てば幸いです

于 2016-06-30T06:42:08.917 に答える