我有一个应用程序,我在其中向滚动视图添加了一些动态数量的按钮。我已经为UIButton
. 由于某些原因,我需要通过以下方式以UIButton
编程方式调用 sender 方法:
[self buttontapped:nil];
就像这样,但它没有通过使用代码来改变按钮的背景:
button.selected = YES;
我最初为按钮设置了这样的背景:
btn = [UIButton buttonWithType:UIButtonTypeCustom];
int j = i+1;
btn.frame = CGRectMake((j-1)*77, 0, 77, 44);
[btn setBackgroundImage:[UIImage imageNamed:@"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"bar_hvr.png"] forState:UIControlStateSelected];
btn.selected = NO;
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:head forState:UIControlStateNormal];
btn.tag = i;
[tabBarS addSubview:btn];
-(void)buttonTapped:(id)sender {
if(sender==nil)
{
btn.tag=0;
}
for(int i=0;i<[sarray count];i++)
{
btn.selected=NO;
}
btn = (UIButton *)sender;
NSLog(@"Tab bar %d is clicked",btn.tag);
[self tabCall:btn.tag];
btn.selected = YES;
}
但是除了改变背景之外的一切都只能工作。我哪里错了?