View Full Version : I'd like to call a function with variable/changeable name.
pannini
Sep 8, 2008, 12:08 PM
Hi, I'm quite new to objective-c and my knowledge is limited. Help would be really appreciated.
Here is the situation, I have a set of UIButton objects, their names are
space11Button, space12Button, space13Button, .... , space99Button
I would like to set the title of a particular button, say, spaceijButton,
when I obtain ij from another integer variable (ie, int index=ij;
when ij is the two digit number).
I can do that by using this command when I know in advance what i and j are
[spaceijButton setTitle:@"My Title";
The problem is, I don't know in advance what i and j would be. Is it possible to write a program to set the title of that particular spaceijButton.
Thank you very much in advance. :)
robbieduncan
Sep 8, 2008, 12:18 PM
1) Buttons don't have names. Those may be the names of variables that happen to point to the buttons, but that is largely neither here nor there.
2) You aren't trying to call a function with a variable name (or more accurately pass a message, this is not procedural C). You are trying to target a variable object. If you had a constant target you could use performSelector: to do what you want (and your title says).
The real answer to your problem is to use an array (or a 2D array) of pointers to the buttons to do this. Of course setting that up in IB might well be impossible. So if you are using IB you may need to connect the outlets as you have them now and assign them to the array when your object initialises from the nib.
pannini
Sep 8, 2008, 12:26 PM
Oh thanks, that's real fast! :)
Thanks for explanation in 1) and 2), I'm hopeless at technical terms.
Do you mind elaborate your answer
The real answer to your problem is to use an array (or a 2D array) of pointers to the buttons to do this. Of course setting that up in IB might well be impossible. So if you are using IB you may need to connect the outlets as you have them now and assign them to the array when your object initialises from the nib.
Right now I'm using IB, I already set up outlets
IBOutlet UIButton *space11Button;
IBOutlet UIButton *space12Button;
IBOutlet UIButton *space13Button;
IBOutlet UIButton *space14Button;
IBOutlet UIButton *space15Button; and so on
As you suggested, I may assign them to an array? Can I write a for loop to do that? If so, could you give me an example?
Thanks again.
robbieduncan
Sep 8, 2008, 12:35 PM
The question will come down to whether you have a 1D or 2D structure of buttons.
If it's 1D something like
NSArray *buttons = [NSArray arrayWithObjects:button1, button2, button3, nil];
will work fine (note this array is autoreleased)
Access is now simple:
[((UIButton *) [buttons objectAtIndex:i]) setTtile:@"Title"];
If it's 2D something like
NSArray *buttons = [NSArray arrayWithObjects:
[NSArray arrayWithObjects:button11, button12, button13,nil],
[NSArray arrayWithObjects:button21, button22, button33,nil],
[NSArray arrayWithObjects:button31, button22, button33,nil],nil];
will work
Access is now a little more complex
[((UIButton *) [((NSArray *) [buttons objectAtIndex:j]) objectAtIndex:i]) setTitle:@"Title"];
In all seriousness if you need this spelt out you need to go and do some reading on basic data structures: this is petty much programming 101.
pannini
Sep 8, 2008, 12:41 PM
I see.
That is elaborate enough. I understand them completely.
My problem was I didn't know about 'arrayWithObjects' and how to use it.:p:p
That's very helpful indeed.
robbieduncan
Sep 8, 2008, 12:43 PM
My problem was I didn't know about 'arrayWithObjects' and how to use it.:p:p
Well, this is perhaps an over-engineered solution. As all the elements are the same type and we know the size of the arrays in advance you could just use a standard 2D C array to do the same thing...
PhoneyDeveloper
Sep 8, 2008, 01:04 PM
Do you really have 99 buttons in one view?
At any rate. You can do this without using 99 outlets. Instead you can set the tags in IB for each control. If you want to set the title of control 34 you can then get the view that matches that tag at runtime and then message it.
Look up [UIView viewWithTag] and the UIView tag property for more info.
pannini
Sep 8, 2008, 11:46 PM
I got it working as you suggested, robbieduncan, thanks.
Instead you can set the tags in IB for each control.
This tag thing might simply be another effective way to do this. I'm going for this, i think. Thanks.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.