NSMenuItem enable items

I have an NSMenuItem with a bunch of elements in it, however ... the list is simply not included.

What I mean:
List

This is my code:

- (void)didFetchNewList:(NSArray *)list { NSArray *smallList = [list subarrayWithRange:NSMakeRange(0, 10)]; NSMenu *menu = [[NSMenu alloc] initWithTitle:@""]; for (NSDictionary *dict in smallList) { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MMM dd @ HH:mm:ss"]; NSMenuItem *soMenuItem = [[NSMenuItem alloc] initWithTitle: [dateFormatter stringFromDate:[dict objectForKey:@"date"]] action:nil keyEquivalent:@""]; [soMenuItem setEnabled:YES]; [menu addItem:soMenuItem]; } [menu addItem:[NSMenuItem separatorItem]]; NSMenuItem *soMenuItem = [[NSMenuItem alloc] initWithTitle:@"Settings" action:nil keyEquivalent:@"S"]; [soMenuItem setEnabled:YES]; [menu addItem:soMenuItem]; [statusItem setMenu:menu]; [statusItem setEnabled:YES]; } 

I set everything as enabled, but it is still disabled. How can i solve this?

+6
source share
1 answer

When creating NSMenuItem your element must have a valid target and a valid selector. This means that the target cannot be zero and must respond to the transmitted selector. Keep in mind that in this case the NULL selector will not include the menu item.

 NSMenu *myMenu; NSMenuItem *myItem; myMenu = [[NSMenu alloc] initWithTitle:@""]; myItem = [[NSMenuItem alloc] initWithTitle:@"Test" action:@selector(validSelector:) keyEquivalent:@""]; [myItem setTarget:myTarget]; [myMenu addItem:myItem]; // Do anything you like [myMenu release]; [myItem release]; 

EDIT: I saw that you call -[NSMenuItem setEnabled:] using YES after creating the menu item. This is optional since they will be enabled by default.

EDIT 2: As indicated by NSGod (see comment below), the target may be zero. In this case, the first responder of your application will receive the passed method. That is, until the first responder implements this method. (edit 3) However, if this is not the case, the method will be sent to the next responder in the responder chain. This continues until a defendant is detected that responds to the selector, or if there are no respondents remaining pending. When no responder is found, the menu item will not be included.

+12
source

Source: https://habr.com/ru/post/905262/


All Articles