![](/img/trans.png)
[英]How do I bind a state of a NSMenuItem (enabled) to a specific value of an NSNumber?
[英]NSMenuItem not being enabled?
我有一個名為history的NSMenuItem,它位於一個名為菜單的NSMenu中。 當我的程序啟動時,歷史記錄沒有子菜單,因此禁用它。 然后在某些時候,我需要一個歷史子菜單,所以我創建它,使它成為歷史的子菜單。 歷史旁邊出現一個箭頭,告訴我子菜單在那里。 但歷史仍然是殘疾人。 我嘗試過setEnabled但不起作用。 請幫忙。 這是我的代碼:
這是我創建菜單和歷史記錄,因為您可以在菜單中看到NSMenuItem。
menu = [[NSMenu alloc] initWithTitle:@"Menu"];
[[menu addItemWithTitle:@"History" action:nil keyEquivalent:@""] setTarget:self];
[[menu addItemWithTitle:@"Settings" action:@selector(loadSettings:) keyEquivalent:@""] setTarget:self];
[[menu addItemWithTitle:@"Quit" action:@selector(terminateApp:) keyEquivalent:@""] setTarget:self];
此時,歷史記錄被禁用(灰色)。 然后在程序的某個地方我需要有一個歷史子菜單,所以:
if (historyMenu == nil) {
historyMenu = [[NSMenu alloc] initWithTitle:@"Lyrics history"];
[menu setSubmenu:historyMenu forItem:[menu itemWithTitle:@"History"]];
}
我現在看到歷史旁邊的一個箭頭,但它仍然是灰色的。
請幫助,我一直試圖弄清楚最后2個小時。 謝謝。
我不得不重寫validateMenuItem
方法以return true
:
override func validateMenuItem(menuItem: NSMenuItem) -> Bool {
return true
}
它實際上並沒有使用驗證方法,直到我將目標設置為self而不像你所做的那樣
menu.addItemWithTitle("An Item", action: #selector(itemPressed), keyEquivalent: "")?.target = self
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.