以编程方式调用ibaction方法不会改变按钮的背景?

时间:2013-06-10 10:47:18

标签: iphone ios ipad uibutton

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

但除了背景改变之外的所有事情都有效。我哪里错了?

5 个答案:

答案 0 :(得分: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];

    }

}

修改

您可以使用bellow trik调用按钮动作事件programeticuly: -

[btn sendActionsForControlEvents:UIControlEventTouchUpInside];

答案 1 :(得分:0)

首先写下此波纹管线,并将方法类型从void更改为IBAction ...

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];
}

答案 2 :(得分:0)

您需要设置图片属性,而不是backgroundImage的{​​{1}}属性。 使用UIButton图片属性和default属性。

selected

答案 3 :(得分:0)

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

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

答案 4 :(得分:-1)

在按钮操作

   btn.selected=YES;
最后一行中的

使按钮处于选定状态

也可以获得正确的实例

btn=(UIButton *)sender;