我是这个iPad业务的新手,我正在基于XML文件的内容以编程方式在scrollview中创建一些按钮。我在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];
现在,我追加的方法监听器工作正常,但按钮的文本从不显示......我做错了什么?
答案 0 :(得分:5)
尝试更改行:
[button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
为:
[button setTitle:titleForButton forState:UIControlStateNormal];
forState参数不能按照您当前的预期工作,它的工作方式如下:
[button setTitle:@"Normal" forState:UIControlStateNormal];
[button setTitle:@"Highlighted" forState:UIControlStateHighlighted];
答案 1 :(得分:1)
另外,如果你只设置了UIControlStateNormal文本..它将是你用逐位OR分隔的所有控制状态的文本,我相信,这是你要搜索的功能。
答案 2 :(得分:1)
[button setTitle:@"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
答案 3 :(得分:0)
如果你想为所有州设置相同的标题,那么这样做:
[button setTitle:titleForButton forState:UIControlStateNormal];
[button setTitle:titleForButton forState:UIControlStateHighlighted];