【问题标题】:Change background image of clicked UIButton created programmatically更改以编程方式创建的单击 UIButton 的背景图像
【发布时间】:2016-02-11 06:01:17
【问题描述】:

我以编程方式创建了UIButtons 并将它们添加到我的UIScrollView。现在我想将点击的UIButton 的背景图像更改为图像,其他图像应保持为默认颜色的蓝色。现在当我点击另一个UIButton,我想将之前点击的UIButton 更改为蓝色,当前点击的按钮有背景图片。

这是创建和设置UIButton属性的代码:

   for (int i=0; i<[arrPartVenuDetails count]; i++) {

        NSString *venueId = [[arrPartVenuDetails objectAtIndex:i] objectForKey:@"venue_id"];
        NSInteger venue_id= [venueId integerValue];
        NSLog(@"venue id %ld",(long)venue_id);

        aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [aButton setFrame:CGRectMake(10.0f, 10.0f, 100.0f, 20.f)];

        [aButton setBackgroundColor:[UIColor colorWithRed:(6/255.0) green:(130/255.0) blue:(195/255.0) alpha:1]];
        NSString *buttonClassName = [NSString stringWithFormat:@"VENUE %d", i+1];
        [aButton.titleLabel setTextAlignment: NSTextAlignmentCenter];

        [aButton setTitle:buttonClassName forState:UIControlStateNormal];
        aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
        [aButton addTarget:self action:@selector(venueButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        aButton.tag = venue_id;
        [scrollVenue addSubview:aButton];

        yCoord += buttonHeight + buffer;
    }

这是按钮点击方法:

-(void) venueButtonClick:(UIButton *)sender{

    [sender setBackgroundImage:[UIImage imageNamed:@"venue .png"] forState:UIControlStateNormal];

    CGRect btnOnclickFrame = sender.frame;
    btnOnclickFrame.size.width = 210;
    sender.frame = btnOnclickFrame;
 }

【问题讨论】:

  • 在按钮的点击事件中,检查它的标签。如果是第一个按钮,则将其背景颜色设置为红色,将所有其他按钮设置为默认蓝色。像这样,您可以对所有其他按钮执行此操作。
  • 您已经在为按钮设置标签。如果您不使用它们,它们有什么用? (虽然事实上我很难学到标签一点都不好)

标签: ios objective-c iphone uibutton


【解决方案1】:

修改你的venueButtonClick如下:

-(void) venueButtonClick:(UIButton *)sender{
     for (UIButton *btn in scrollVenue) {

        if (btn.tag==sender.tag) {

            [btn setBackgroundImage:[UIImage imageNamed:@"venue .png"] forState:UIControlStateNormal];
        }
        else{

            [btn setBackgroundColor:[UIColor colorWithRed:(6/255.0) green:(130/255.0) blue:(195/255.0) alpha:1]];
        }
    }
}

【讨论】:

  • 我可以用for (UIButton *aButton) 替换for (UIButton *btn in scrollVenue) 吗?
  • 是的,你可以给任何名字并在venueButtonClick方法中添加这个for循环。
  • 多么可怕的循环使用方式!
【解决方案2】:

将新属性添加到您的 .h 文件中。

@property NSUInteger lastSelectedTag;

在您的viewDidLoad 中,为其分配一些安全值:

_lastSelectedTag = 80807652;//example value as it will be unique

现在在您的IBAction 中,将其更改为:

-(void) venueButtonClick:(UIButton *)sender{

    if(_lastSelectedTag!=80807652)
    {
        //lastSelectedTag is different than our safe value so it means that it now contains tag of real last selected button
        UIButton *lastSelectedButton = [scrollVenue viewWithTag:_lastSelectedTag]; //This is the previously selected button

        //Change it to blue and do whatever you want
       [lastSelectedButton setBackgroundImage:nil forState:UIControlStateNormal];//remove background image
       [lastSelectedButton setBackgroundColor:[UIColor colorWithRed:(6/255.0) green:(130/255.0) blue:(195/255.0) alpha:1]]; //give it color

    }
   [sender setBackgroundImage:[UIImage imageNamed:@"venue .png"] forState:UIControlStateNormal];

    CGRect btnOnclickFrame = sender.frame;
    [aButton setBackgroundColor:[UIColor clearColor]];
    btnOnclickFrame.size.width = 210;
    sender.frame = btnOnclickFrame;
    _lastSelectedTag = sender.tag; //Set lastSelectedButton to this button's tag
}

顺便提个建议,你最好使用UITableView 而不是UIScrollView。

【讨论】:

    【解决方案3】:

    还有两种可能:

    1. 如果您要创建 3-4 按钮,最好使用 UISegmentedControl,这将允许您对其进行自定义,而无需编写所有额外的代码。

    See the apple docs for details on how to customise

    1. 如果您创建许多按钮,那么UITableView 可能更合适,并使用UITableViewCell 的选定属性,您将需要创建一个自定义UITableViewCell。

    希望这会有所帮助。

    【讨论】:

    • 呃,没有。他正在根据数组的数量创建无限数量的按钮。虽然在这种情况下使用 scrollView 确实是不正确的,但是您的建议是不正确的。 UITableView 是需要的
    • 假设将有 15 个按钮。你将如何适应 iPhone 宽度分段控件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 2011-02-02
    • 1970-01-01
    相关资源
    最近更新 更多