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

pannini

macrumors newbie
Original poster
Sep 8, 2008
5
0
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
Code:
[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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors newbie
Original poster
Sep 8, 2008
5
0
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
Code:
	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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
The question will come down to whether you have a 1D or 2D structure of buttons.

If it's 1D something like

Code:
NSArray *buttons =  [NSArray arrayWithObjects:button1, button2, button3, nil];
will work fine (note this array is autoreleased)

Access is now simple:
Code:
[((UIButton *) [buttons objectAtIndex:i]) setTtile:@"Title"];

If it's 2D something like
Code:
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
Code:
[((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

macrumors newbie
Original poster
Sep 8, 2008
5
0
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors 68040
Sep 2, 2008
3,114
93
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

macrumors newbie
Original poster
Sep 8, 2008
5
0
I got it working as you suggested, robbieduncan, thanks.


Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.