One simple doubt Object-c example

Discussion in 'Mac Programming' started by diogosgp, Jul 6, 2011.

  1. diogosgp, Jul 6, 2011
    Last edited by a moderator: Jul 6, 2011

    diogosgp macrumors newbie

    Joined:
    Jun 26, 2011
    #1
    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
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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.
     
  3. diogosgp thread starter macrumors newbie

    Joined:
    Jun 26, 2011
    #3
    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
     
  4. jiminaus, Jul 6, 2011
    Last edited: Jul 6, 2011

    jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    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?
     

Share This Page