第一:我是Swift发展的新手
我正在使用this教程制作一个弹出窗口,当我点击我的顶部栏图标时会出现。
所以我的AppDelehgate代码是:
func applicationDidFinishLaunching(_ aNotification: Notification){
print("START !");
if let button = statusItem.button {
button.image = NSImage(named: "logo");
button.action = Selector(("togglePopover:"));
//button.target = self;
}
popover.contentViewController = QuotesViewController(nibName: "QuotesViewController", bundle: nil);
}
func togglePopover(envoyeur: AnyObject?){
print("TOGGLE !");
if popover.isShown{
closePopover(sender: envoyeur)
} else {
showPopover(sender: envoyeur)
}
}
但绝不是“TOGGLE!”文字出现,事件不起作用 我把“button.target = self;”评论,因为如果我不这样做它会崩溃。事件似乎被触发,但我有一个错误:
START !
2017-06-08 15:55:29.116275+0200 MyWallpaper[7590:407660] -[MyWallpaper.AppDelegate togglePopover:]: unrecognized selector sent to instance 0x61000002a520
2017-06-08 15:55:29.122570+0200 MyWallpaper[7590:407660] [General] -[MyWallpaper.AppDelegate togglePopover:]: unrecognized selector sent to instance 0x61000002a520
2017-06-08 15:55:29.125822+0200 MyWallpaper[7590:407660] [General] (
0 CoreFoundation 0x00007fffb36f32cb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffc84fe48d objc_exception_throw + 48
2 CoreFoundation 0x00007fffb3774f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fffb3665755 ___forwarding___ + 1061
4 CoreFoundation 0x00007fffb36652a8 _CF_forwarding_prep_0 + 120
5 libsystem_trace.dylib 0x00007fffc90153a7 _os_activity_initiate_impl + 53
6 AppKit 0x00007fffb18e1721 -[NSApplication(NSResponder) sendAction:to:from:] + 456
7 AppKit 0x00007fffb13c5cc4 -[NSControl sendAction:to:] + 86
8 AppKit 0x00007fffb18ceff7 -[NSStatusBarButtonCell _sendActionFrom:] + 155
9 libsystem_trace.dylib 0x00007fffc90153a7 _os_activity_initiate_impl + 53
10 AppKit 0x00007fffb13c4426 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2481
11 AppKit 0x00007fffb1408272 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 798
12 AppKit 0x00007fffb18cea1f -[NSStatusBarButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 327
13 AppKit 0x00007fffb13c2ddb -[NSControl mouseDown:] + 832
14 AppKit 0x00007fffb1a5d24f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341
15 AppKit 0x00007fffb1a59a6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942
16 AppKit 0x00007fffb1a58f0a -[NSWindow(NSEventRouting) sendEvent:] + 541
17 AppKit 0x00007fffb18d26d1 -[NSStatusBarWindow sendEvent:] + 581
18 AppKit 0x00007fffb18dd681 -[NSApplication(NSEvent) sendEvent:] + 1145
19 AppKit 0x00007fffb1158427 -[NSApplication run] + 1002
20 AppKit 0x00007fffb1122e0e NSApplicationMain + 1237
21 MyWallpaper 0x00000001000039ad main + 13
22 libdyld.dylib 0x00007fffc8de3235 start + 1
23 ??? 0x0000000000000003 0x0 + 3
)
答案 0 :(得分:1)
使用Swift #selector
语法
button.action = #selector(togglePopover(_:)) // or just #selector(togglePopover)