Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

diogosgp

macrumors newbie
Original poster
Jun 26, 2011
18
0
Hi guys, I'm learning object-c with cocoa. I was studying this example and I a question has appeared in my mind. Follow the code:


Code:
-(IBAction)findSelectedButton: (id)senderes
{
	[COLOR="Red"]NSPopUpButtonCell *selCell = [senderes selectedCell];[/COLOR]
	NSLog(@"Selected cell is %d", selCell.tag);
	NSLog(@"Title is %@", selCell.title);
	NSInteger index = [senderes indexOfSelectedItem];
	NSLog(@"Selected item is %i", index);
}
I didn't understand why I have to use "selectedCell". Why only senderes?
Don't the code identifies that I'm selecting a option when I click on one item from the Pop button?

Cheers
 
Last edited by a moderator:
Here's another way to code the same thing, which might be insightful.

Code:
-(IBAction)findSelectedButton: (id)senderes
{
  NSInteger index = [senderes indexOfSelectedItem];
  NSLog(@"Selected item is %ld", (long)index);
  NSLog(@"Title (from senderes) is %@", [senderes titleOfSelectedItem]);
  NSMenuItem* selMenuItem = [senderes selectedItem];
  NSLog(@"Title (from menu item) is %@", [selMenuItem title]);
  NSLog(@"Tag (from menu item) is %ld", (long)[selMenuItem tag]);
}

Note that NSPopUpButton doesn't have tagOfSelectedItem.

Personally I think getting the title and tag from the menu item is more kosher then getting it from the cell.
 
Here's another way to code the same thing, which might be insightful.

Code:
-(IBAction)findSelectedButton: (id)senderes
{
  NSInteger index = [senderes indexOfSelectedItem];
  NSLog(@"Selected item is %ld", (long)index);
  NSLog(@"Title (from senderes) is %@", [senderes titleOfSelectedItem]);
  NSMenuItem* selMenuItem = [senderes selectedItem];
  NSLog(@"Title (from menu item) is %@", [selMenuItem title]);
  NSLog(@"Tag (from menu item) is %ld", (long)[selMenuItem tag]);
}

Note that NSPopUpButton doesn't have tagOfSelectedItem.

Personally I think getting the title and tag from the menu item is more kosher then getting it from the cell.
Thank you, but my question is why can't I only use NSPopUpButtonCell *selCell = [senderes];

Why don't the program recognise that senderes is what I've selected?

Cheers
 
Thank you, but my question is why can't I only use NSPopUpButtonCell *selCell = [senderes];

Why don't the program recognise that senderes is what I've selected?

Cheers

Because it doesn't work that way.

EDIT: Actually it doesn't normally work this way. Where is this action connected to, the NSPopUpButton or the NSPopUpButtonCell inside the NSPopUpButton?
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.