- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的 Mac 应用程序有一个 NSMenu,其委托(delegate)函数 validateMenuItem
和 menuWillOpen
从未被调用。到目前为止,在线解决方案均无济于事。
看起来我做的一切都是对的:
我想描述我的问题的最好方法是发布相关代码。任何帮助将不胜感激。
import Cocoa
class UIManager: NSObject, NSMenuDelegate {
var statusBarItem = NSStatusBar.system().statusItem(withLength: -2)
var statusBarMenu = NSMenu()
var titleMenuItem = NSMenuItem()
var descriptionMenuItem = NSMenuItem()
// ...
override init() {
super.init()
createStatusBarMenu()
}
// ...
func createStatusBarMenu() {
// Status bar icon
guard let icon = NSImage(named: "iconFrame44")
else { NSLog("Error setting status bar icon image."); return }
icon.isTemplate = true
statusBarItem.image = icon
// Create Submenu items
let viewOnRedditMenuItem = NSMenuItem(title: "View on Reddit...", action: #selector(viewOnRedditAction), keyEquivalent: "")
let saveThisImageMenuItem = NSMenuItem(title: "Save This Image...", action: #selector(saveThisImageAction), keyEquivalent: "")
// Add to title submenu
let titleSubmenu = NSMenu(title: "")
titleSubmenu.addItem(descriptionMenuItem)
titleSubmenu.addItem(NSMenuItem.separator())
titleSubmenu.addItem(viewOnRedditMenuItem)
titleSubmenu.addItem(saveThisImageMenuItem)
// Create main menu items
titleMenuItem = NSMenuItem(title: "No Wallpaperer Image", action: nil, keyEquivalent: "")
titleMenuItem.submenu = titleSubmenu
getNewWallpaperMenuItem = NSMenuItem(title: "Update Now", action: #selector(getNewWallpaperAction), keyEquivalent: "")
let preferencesMenuItem = NSMenuItem(title: "Preferences...", action: #selector(preferencesAction), keyEquivalent: "")
let quitMenuItem = NSMenuItem(title: "Quit Wallpaperer", action: #selector(quitAction), keyEquivalent: "")
// Add to main menu
let statusBarMenu = NSMenu(title: "")
statusBarMenu.addItem(titleMenuItem)
statusBarMenu.addItem(NSMenuItem.separator())
statusBarMenu.addItem(getNewWallpaperMenuItem)
statusBarMenu.addItem(NSMenuItem.separator())
statusBarMenu.addItem(preferencesMenuItem)
statusBarMenu.addItem(quitMenuItem)
statusBarItem.menu = statusBarMenu
}
// ...
// Called whenever the menu is about to show. we use it to change the menu based on the current UI mode (offline/updating/etc)
override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
NSLog("Validating menu item")
if (menuItem == getNewWallpaperMenuItem) {
if wallpaperUpdater!.state == .Busy {
DispatchQueue.main.async {
self.getNewWallpaperMenuItem.title = "Updating Wallpaper..."
}
return false
} else if wallpaperUpdater!.state == .Offline {
DispatchQueue.main.async {
self.getNewWallpaperMenuItem.title = "No Internet Connection"
}
return false
} else {
DispatchQueue.main.async {
self.preferencesViewController.updateNowButton.title = "Update Now"
}
return true
}
}
return true
}
// Whenever the menu is opened, we update the submitted time
func menuWillOpen(_ menu: NSMenu) {
NSLog("Menu will open")
if !noWallpapererImageMode {
DispatchQueue.main.async {
self.descriptionMenuItem.title = "Submitted \(self.dateSimplifier(self.updateManager!.thisPost.attributes.created_utc as Date)) by \(self.updateManager!.thisPost.attributes.author) to /r/\(self.updateManager!.thisPost.attributes.subreddit)"
}
}
}
// ...
// MARK: User-initiated actions
func viewOnRedditAction() {
guard let url = URL(string: "http://www.reddit.com\(updateManager!.thisPost.permalink)")
else { NSLog("Could not convert post permalink to URL."); return }
NSWorkspace.shared().open(url)
}
// Present a save panel to let the user save the current wallpaper
func saveThisImageAction() {
DispatchQueue.main.async {
let savePanel = NSSavePanel()
savePanel.makeKeyAndOrderFront(self)
savePanel.nameFieldStringValue = self.updateManager!.thisPost.id + ".png"
let result = savePanel.runModal()
if result == NSFileHandlingPanelOKButton {
let exportedFileURL = savePanel.url!
guard let lastImagePath = UserDefaults.standard.string(forKey: "lastImagePath")
else { NSLog("Error getting last post ID from persistent storage."); return }
let imageData = try! Data(contentsOf: URL(fileURLWithPath: lastImagePath))
if (try? imageData.write(to: exportedFileURL, options: [.atomic])) == nil {
NSLog("Error saving image to user-specified folder.")
}
}
}
}
func getNewWallpaperAction() {
updateManager!.refreshAndReschedule(userInitiated: true)
}
func preferencesAction() {
preferencesWindow.makeKeyAndOrderFront(nil)
NSApp.activateIgnoringOtherApps(true)
}
func quitAction() {
NSApplication.shared().terminate(self)
}
}
最佳答案
menuWillOpen:
属于NSMenuDelegate
协议(protocol);要调用有问题的菜单,需要一个委托(delegate):
let statusBarMenu = NSMenu(title: "")
statusBarMenu.delegate = self
validateMenuItem:
属于NSMenuValidation
非正式协议(protocol);要调用相关菜单 items 必须有一个 target
。以下这段话摘自Apple的Application Menu and Pop-up List Programming Topics文档:
When you use automatic menu enabling, NSMenu updates the status of every menu item whenever a user event occurs. To update the status of a menu item, NSMenu first determines the target of the item and then determines whether the target implements validateMenuItem: or validateUserInterfaceItem: (in that order).
let myMenuItem = NSMenuItem()
myMenuItem.target = self
myMenuItem.action = #selector(doSomething)
关于swift - 不为 NSMenu 调用 validateMenuItem 或 menuWillOpen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38461449/
这是一个最佳实践问题: 我有许多菜单项,这些菜单项根据关联 NSTableView 中选择的行数进行切换。例如,仅当选择了两行时才允许“连接结束”,而不是零,不是一,只有两行。 我的问题是如何在 va
我的主菜单有几个菜单项(文件、编辑、 View 、窗口等等)。所有菜单项都将其操作设置为 FirstResponder 中的操作。 应用程序有一个窗口,该窗口的类型为 MyWindow,它继承自 NS
这是我的示例类和用法: @interface CCocoaMenuItem : NSMenuItem { someClass *someobj; } - (void)menuEventHand
我正在尝试使用 Storyboard 设置菜单栏应用程序,但未调用我的 validateMenuItem 方法。 我会尽力解释我做了什么。首先,我在我的应用程序场景中拖动了一个菜单项。然后是我的 Me
我有一个类,MyViewController,有几个从菜单项触发的 Action 。 class MyViewController: NSViewController { … } 这些操作与 IB 中
我的 Mac 应用程序有一个 NSMenu,其委托(delegate)函数 validateMenuItem 和 menuWillOpen 从未被调用。到目前为止,在线解决方案均无济于事。 看起来我做
我是一名优秀的程序员,十分优秀!