0

There are 24 buttons that their labels change from a function driven by a UISegmentedControl that has 7 segments. There is also a UIPickerView with 9 different objects. These two dynamically change the labels of the buttons. Then, when a button is pressed i have to know the UIPickerView object that is selected, the UISegmentedControl selection and which button is this.

My function that receives and executes is this:

- (int)ButtonPlayController:(int)buttonPressed {

   NSString *button = [NSString stringWithFormat:@"%@%i", @"button", buttonPressed];

   if (button.titleLabel.text == @"1C") {

       [self Play1C];

   }

  else if .....

}

The function receives a number (the button number) and combines the string name to create the UIButton name, e.g. if the function receives number 8 then the UIButton name is button8 (i already have on my .h file: IBOutlet UIButton *button8;)

I remember these work on PHP but what about Object C? What do i have to change for this to work?

castbin
  • 3
  • 2
  • possible duplicate of [variable name from string in obj-c](http://stackoverflow.com/questions/3164860/variable-name-from-string-in-obj-c) – jscs Dec 04 '11 at 22:10
  • Also: [Obj-C equivalent of PHP's "variable variables"](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables). Also also: http://stackoverflow.com/questions/8090590/is-this-possible-to-call-variable-dynamically-in-objective-c and questions linked from there. – jscs Dec 04 '11 at 22:11

1 Answers1

0

Looks like there some pointers for you in the comments, but I'm wondering why you are implementing your target-action this way? Maybe there is a part of your program I don't understand but why not just pass the button as the argument to the receiver of the action:

- (IBAction)doStuffWithButton:(id)sender
{
    if ( sender.tag == 1 )
    {
        [self actOnOne];
    }
    else if ( sender.tag == 2 )
    {
        ....
    }
}

Like I say, this doesn't answer your original question, but maybe a different design pattern would make things easier?

D.C.
  • 15,340
  • 19
  • 71
  • 102
  • My problem is that the labels of the buttons change dynamically from another function. Ok, let me fully explain. There are 24 buttons that their labels change from a function driven by a UISegmentedControl that has 7 segments. There is also a UIPickerView with 9 different objects. – castbin Dec 04 '11 at 22:59
  • These two dynamically change the labels of the buttons. Then, when a button is pressed i have to know the UIPickerView object that is selected, the UISegmentedControl selection and which button is this. – castbin Dec 04 '11 at 23:06
  • I used a combination of the tag + Label name! Thank you! – castbin Dec 04 '11 at 23:32