get NSButton IB id as variable (Obj. C)

Discussion in 'Mac Programming' started by RobRiley, Mar 15, 2009.

  1. macrumors member

    Joined:
    Feb 4, 2009
    Location:
    London
    #1
    Hi,

    I've written a routine that I want to execute differently depending on which button calls it. For example - if senderID = buttonA NSString = A.. etc

    Can anyone tell me what the method and correct syntax for this is? I literally just want to get the Interface Builder ID of a button and put it in a variable when the button is clicked.

    Many thanks,
     
  2. macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    You can either assign a tag to the button and access it using the -tag method, or you can hook up each of your buttons to IBOutlets and check for equality with ==.
     
  3. thread starter macrumors member

    Joined:
    Feb 4, 2009
    Location:
    London
    #3
    Great, thanks. This sounds like what I need. Can you give me an example of syntax? Sorry I'm still quite new to Objective C.. For example, I guess I would need to declare a variable for the tag value in my header and then check that value in my implementation to determine what code to execute? Hope I'm making sense.. :)

    Thanks again.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    The tag can be set in IB, and then you can do something like this:
    Code:
    int tag = [button tag];
    if (tag == 0)
        // do something
    else if (tag == 1)
        // do something else
    If you have a lot of buttons, you could use a switch statement instead. Depends on what you're doing.
     
  5. thread starter macrumors member

    Joined:
    Feb 4, 2009
    Location:
    London
    #5
    Great, that makes sense. But should I be substituting 'button' for something relevant to my app? And should the tag variable be in the header?

    Thanks :)
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Yes, rename it to whatever you name your own button. It is just an example.

    No, variables can be declared locally, and should be if they are only used in that method. If you need to access a variable across different methods in a class, declare it in @interface instead.
     
  7. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #7
    Is there a rule of thumb for when it makes sense to use the tag method described here as opposed to connecting button outlets in IB (the technique that's typically taught in tutorials)?
     
  8. thread starter macrumors member

    Joined:
    Feb 4, 2009
    Location:
    London
    #8
    Thanks again kainjow :)

    But it now complains that I haven't declared the variable. For example my code is like this:
    Code:
    int tag = [TaskSender tag];
    if (tag == 1)
    	//do some stuff
    else if (tag == 2)
    	//do some other stuff
    
    When I compile I get 'error: 'TaskSender' undeclared (first use in this function)'. TaskSender is what I have called my buttons in IB in both 'Description' under Accessibility Identity and 'Name' under Interface Builder Identity. Have I named it in the wrong place? Sorry to be dumb - I can't seem to get my brain out of PHP mode!

    Cheers.
     
  9. thread starter macrumors member

    Joined:
    Feb 4, 2009
    Location:
    London
    #9
    I'm a complete noobie here so it's possible my approach is completely wrong. But my reason for doing it this way is to avoid re-writing code - I have a method that calls executables using NSTask and I want it to call different executables depending on which button is clicked. The only difference for each click of a button is a single variable (the name of the executable file) so I don't want to repeat practically the same method for each individual button.
     
  10. macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #10
    int tag = [sender tag];

    TaskSender is an instance that exists in the nib. It doesn't mean anything to the app code.
     
  11. thread starter macrumors member

    Joined:
    Feb 4, 2009
    Location:
    London
    #11
    That did it!

    Thanks so much for all your help/feedback everyone.
     
  12. thread starter macrumors member

    Joined:
    Feb 4, 2009
    Location:
    London
    #12
    For anyone reading this, the code that worked for me is:
    Code:
    int tag = [sender tag];
    if (tag == 1)
    //do some stuff		
    else if (tag == 2)
    //do some other stuff
    
    ..where of course the buttons are tagged '1' and '2' in IB.
     

Share This Page