【问题标题】:How to change image for UIButton using switch statement?如何使用 switch 语句更改 UIButton 的图像?
【发布时间】:2014-12-15 03:43:59
【问题描述】:

我能够在我的数组中的 UIButton 中添加一个“setbackgroundimage”。现在所有按钮都有相同的图像。如何创建一个 switch 语句,以便能够为我的 NSarray 中的每个 initwithobjects @"" 设置不同的图像?

下面是我正在使用的代码:

 menuItems = [[NSArray alloc]initWithObjects:@"",@"",@"",@"",@"",@"",@"", nil];
    for (int b=0;b<[menuItems count];b++) {

        UIButton *mybutton = [[UIButton alloc]initWithFrame:CGRectMake(3.0f, originofButtons, buttonWidth, buttonHeight)];
        //mybutton.backgroundColor = [UIColor redColor];
        [mybutton setBackgroundImage:[UIImage imageNamed:@"menubutton.png"]
         forState:UIControlStateNormal];
        [mybutton setTag:b];
        [mybutton setTitle:[menuItems objectAtIndex:b] forState:UIControlStateNormal];
        [mybutton setSelected:false];
        [mybutton addTarget:self action:@selector(buttonpress:) forControlEvents:UIControlEventTouchUpInside];

        [m_scrollview addSubview:mybutton];

        originofButtons += (buttonHeight + buttonseparator); 
        }

【问题讨论】:

  • [mybutton setBackgroundImage:[UIImage imageNamed:@"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];
  • 使用switch(b){} 并为每个案例设置图像。那里有什么问题?
  • 嘿@WorldOfWarcraft。我已经有那条线了。我已经设置了图像。但我在所有按钮上都得到了相同的按钮。我想知道如何给同一个数组中的其他按钮一个不同的图像

标签: ios uibutton nsarray switch-statement


【解决方案1】:

设置选中的图片:

[mybutton setBackgroundImage:[UIImage imageNamed:@"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];

如果你想设置不同的,你可以链接tag

switch (myButton.tag) {
        case 0:
            [mybutton setBackgroundImage:[UIImage imageNamed:@"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];
            break;
        case 1:
            [mybutton setBackgroundImage:[UIImage imageNamed:@"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];
            break;
    }

【讨论】:

  • 你就是男人!那行得通。太感谢了!在 if 语句中,如果我的状态被选为,这有关系吗?
  • 你应该使用 if-else 语句,因为标签只能是一个值。
  • 不要使用 if...else 条件,最好使用switch() 语句,因为标签是一个整数值
  • @Miguel 其实你可以随意设置 controlState。
【解决方案2】:

这可以为您的场景提供帮助。 您可以在 NSArray 中添加按钮图像名称,然后使用以下代码 sn-p 为每种情况设置图像:

NSArray*  menuItems = [[NSArray alloc]initWithObjects:@"FirstScreen",@"SecondScreen",@"ThridScreen", nil];

for (int b=0;b<[menuItems count];b++) {

    UIButton *mybutton = [[UIButton alloc]init ];//WithFrame:CGRectMake(3.0f, originofButtons, buttonWidth, buttonHeight)];
    [mybutton setBackgroundImage:[UIImage imageNamed:[menuItems[b] stringByAppendingString:@".png"]] forState:UIControlStateSelected]; 

【讨论】:

    【解决方案3】:

    我希望这将根据您的要求对您有所帮助.....

    menuItems = [[NSArray alloc]initWithObjects:@"",@"",@"",@"",@"",@"",@"", nil];
    for (int b=0;b<[menuItems count];b++) {
    
        UIButton *mybutton = [[UIButton alloc]initWithFrame:CGRectMake(3.0f, originofButtons, buttonWidth, buttonHeight)];
        //mybutton.backgroundColor = [UIColor redColor];
        [mybutton setBackgroundImage:[self getImageFromIndex:b %4]
         forState:UIControlStateNormal];
        [mybutton setTag:b];
        [mybutton setTitle:[menuItems objectAtIndex:b] forState:UIControlStateNormal];
        [mybutton setSelected:false];
        [mybutton addTarget:self action:@selector(buttonpress:) forControlEvents:UIControlEventTouchUpInside];
    
        [m_scrollview addSubview:mybutton];
    
        originofButtons += (buttonHeight + buttonseparator);
    
    
    
        }
    
    
    -(UIImage *) getImageFromIndex:(short) index
    {
    switch (index) {
        case 0:
            return [UIImage imageNamed:@"card_01.png"];
            break;
        case 1:
            return [UIImage imageNamed:@"card_02.png"];
            break;
        case 2:
            return [UIImage imageNamed:@"card_03.png"];
            break;
        case 3:
            return [UIImage imageNamed:@"card_04.png"];
            break;
    
        default:
            return [UIImage imageNamed:@"card_01.png"];
            break;
    }
    return nil;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 2021-02-28
      相关资源
      最近更新 更多