views:

52

answers:

3

Hi... so I'm new to this iPad business, and I'm creating some buttons inside a scrollview programatically based on the contents of an XML file. I have this code on a for:

float x = (SLIDER_ELEMENT_HEIGHT * i) + 20;
CGRect frame = CGRectMake(x, 0, SLIDER_ELEMENT_WIDTH, SLIDER_ELEMENT_HEIGHT);
UIButton *button = [[UIButton alloc] initWithFrame:frame];

UIColor *bgColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
button.backgroundColor = bgColor;

NSString *titleForButton = [NSString stringWithFormat: @"This is my title"];
[button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];

UIColor *fgColor = [[UIColor alloc] initWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
[button setTitleColor:fgColor forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];

[button addTarget:self action:@selector(buttonMethod:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[scrl_lastIssues addSubview:button];

Now, the method listener I'm appending is working OK, but the text of the button never shows up... what am I doing wrong?

Thanks for your help!

+2  A: 

Try change the line:

  [button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];

to:

  [button setTitle:titleForButton forState:UIControlStateNormal];

the forState parameter does not work the way you currently expected, it works like:

  [button setTitle:@"Normal" forState:UIControlStateNormal];
  [button setTitle:@"Highlighted" forState:UIControlStateHighlighted];
ohho
Thanks, that solved my problem! I just had seen a forum where they used that line to set the text.
The WebMacheter
A: 

And if you want to set the same title for all the states then do this:

[button setTitle:titleForButton forState:UIControlStateNormal];
[button setTitle:titleForButton forState:UIControlStateHighlighted];
Ideveloper
+1  A: 

Also, if you only set the UIControlStateNormal text..it will be the text for all the control states that you have separated by bit wise ORs, which is, I believe, the functionality you are searching for.

Jesse Naugher