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

RobRiley

macrumors member
Original poster
Feb 4, 2009
35
0
London
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,
 

kpua

macrumors 6502
Jul 25, 2006
294
0
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 ==.
 

RobRiley

macrumors member
Original poster
Feb 4, 2009
35
0
London
assign a tag to the button and access it using the -tag method

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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

RobRiley

macrumors member
Original poster
Feb 4, 2009
35
0
London
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

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 :)
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

GorillaPaws

macrumors 6502a
Oct 26, 2003
932
8
Richmond, VA
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)?
 

RobRiley

macrumors member
Original poster
Feb 4, 2009
35
0
London
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.
 

RobRiley

macrumors member
Original poster
Feb 4, 2009
35
0
London
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)?

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.
 

Saladinos

macrumors 68000
Feb 26, 2008
1,845
4
int tag = [sender tag];

TaskSender is an instance that exists in the nib. It doesn't mean anything to the app code.
 

RobRiley

macrumors member
Original poster
Feb 4, 2009
35
0
London
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.