Search code examples
objective-cmacosnsmenuitem

How to get NSString value from NSPopupButton dropdown list?


Get username values from sqlite db.

-(NSArray*)getUname
{ 

NSArray *resul = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
 NSArray *fetchedRecords = [resul valueForKey:@"uName"];
}

Insert this array values into NSPopupButton

[_registeredUserPopupButton addItemsWithTitles:[self getUname]];

Get a string value from NSPopupButton dropdown list

NSString *usrNam = [NSString stringWithFormat:@"%@",[_registeredUserPopupButton selectedItem]];

From the above code usrNam value returns like below

"NSMenuItem: 0x6080000a9c00 mickel"

but i want my nsstring output as "mickel"


Solution

  • Just get the title from the menu item:

    NSString *usrNam = [[_registeredUserPopupButton selectedItem] title];
    

    or with dot notation

    NSString *usrNam = _registeredUserPopupButton.selectedItem.title;