1

我有一个应用程序,我在其中向滚动视图添加了一些动态数量的按钮。我已经为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; 
}

但是除了改变背景之外的一切都只能工作。我哪里错了?

4

5 回答 5

1

我完成了以下方法调用您的 UIButton 方法,例如:-

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 addTarget:self action:@selector(yourButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor clearColor];
    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    btn.titleLabel.textColor = [UIColor whiteColor];


    btn.tag = i;
    [tabBarS addSubview:btn];

它的动作方法应该和发件人一样

-(IBAction)yourButtonAction:(id)sender
{
    UIButton *btn = (UIButton*)sender;

    if (![btn isSelected])
    {
        [btn setImage:[UIImage imageNamed:@"selected_fb_btn.png"] forState:UIControlStateNormal];
        [btn setSelected:YES];

        [self login];
    }
    else{
        [btn setImage:[UIImage imageNamed:@"facebook.png"] forState:UIControlStateNormal];
        [btn setSelected:NO];

    }

}

编辑

您可以使用以下技巧以编程方式调用按钮操作事件:-

[btn sendActionsForControlEvents:UIControlEventTouchUpInside];

于 2013-06-10T12:54:33.627 回答
0

首先写下下面这行,并将方法类型从更改voidIBAction...

btn = (UIButton *)sender;

像下面...

-(IBAction)buttonTapped:(id)sender {

    btn = (UIButton *)sender;
    if(sender==nil)
    {
     btn.tag=0;
    }

    for(int i=0;i<[sarray count];i++)
    {
       [btn setSelected:NO];
     }
    NSLog(@"Tab bar %d is clicked",btn.tag);
    [self tabCall:btn.tag];
    [btn setSelected:YES];
}
于 2013-06-10T11:05:27.117 回答
0

您需要设置图像属性而backgroundImage不是UIButton. 使用defaultImage 属性和selected属性。

[btn setSelected:TRUE];
于 2013-06-10T11:09:12.150 回答
0

您正在打印的日志是否正确打印了 button.tag?

NSLog(@"标签栏%d被点击",btn.tag);

于 2013-06-10T11:46:25.737 回答
-1

在您的按钮操作中

   btn.selected=YES;

在最后一行使按钮始终处于选中状态

也得到正确的实例

btn=(UIButton *)sender;
于 2013-06-10T11:04:32.833 回答