24

プログラムで NSMenuItem を作成しましたが、無効になっています。validateMenuItem: メソッドをオーバーライドして、すべての項目に対して YES を返すと、メニュー項目は正常に機能します。

メニューに autoEnableItems を指示し、validateMenuItem: メソッドをオーバーライドしないと有効にならないのはなぜですか?

NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:label action:@selector(methodToLogSomething:) keyEquivalent:@""];
menuItem.representedObject = representedObject;
[menuItem setTarget:self];
[menu insertItem:menuItem atIndex:[[menu itemArray] count]-1];
[menuItem release];
[menu setAutoenablesItems:YES];
4

1 に答える 1

45

ターゲットとアクションが適切に設定されていない場合、メニュー項目は無効のままになるようです。私の場合、:セレクターの最後に を忘れていました。

于 2011-01-13T17:15:59.383 に答える