PDA

View Full Version : get NSButton IB id as variable (Obj. C)




RobRiley
Mar 15, 2009, 03:26 PM
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
Mar 15, 2009, 04:11 PM
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
Mar 17, 2009, 04:49 PM
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
Mar 17, 2009, 09:19 PM
The tag can be set in IB, and then you can do something like this:
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
Mar 18, 2009, 02:44 PM
The tag can be set in IB, and then you can do something like this:
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
Mar 18, 2009, 03:00 PM
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
Mar 18, 2009, 03:08 PM
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
Mar 18, 2009, 04:13 PM
Thanks again kainjow :)

But it now complains that I haven't declared the variable. For example my code is like this:

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
Mar 18, 2009, 04:20 PM
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
Mar 18, 2009, 06:29 PM
int tag = [sender tag];

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

RobRiley
Mar 19, 2009, 03:35 AM
int tag = [sender tag];

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

That did it!

Thanks so much for all your help/feedback everyone.

RobRiley
Mar 19, 2009, 03:48 AM
For anyone reading this, the code that worked for me is:

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.