【问题标题】:Change label background color...first red, then yellow on second click更改标签背景颜色...首先是红色,然后是第二次单击时的黄色
【发布时间】:2014-09-10 01:52:14
【问题描述】:

我找不到任何与之相关的东西。我是初学者,但我假设我需要一个 NSArray 来更改颜色。

无论如何,我有一个按钮,当您单击该按钮时,它会将标签背景更改为绿色。我如何做到这一点,所以第一次单击它会将标签背景更改为绿色......然后第二次单击它将背景更改为黄色

- (IBAction)studentOne:(id)sender {
_studentOne.backgroundColor = [UIColor greenColor];
_studentOne.backgroundColor = [UIColor yellowColor];
_studentOne.backgroundColor = [UIColor redColor];
_studentOne.backgroundColor = [UIColor blackColor];
_studentOne.backgroundColor = [UIColor whiteColor];


}

这将是我的顺序。

会不会是涉及到的事情

NSArray *colorArray;
colorArray = [NSArray array with objects:

我尝试了这个,因为我认为这可能就像在单击按钮时更改文本颜色

- (IBAction)studentOne:(id)sender {
NSArray *colorArray;
colorArray = [NSArray arrayWithObjects:

@"_studentOne.backgroundColor = [UIColor greenColor]",
@"_studentOne.backgroundColor = [UIColor yellowColor]",
@"_studentOne.backgroundColor = [UIColor redColor]",
@"_studentOne.backgroundColor = [UIColor blackColor]",
@"_studentOne.backgroundColor = [UIColor whiteColor]",
              nil];


}

【问题讨论】:

    标签: ios xcode background


    【解决方案1】:

    您对数组的看法是正确的,只是在语法上有点偏离。这样的事情会起作用:

    - (IBAction)studentOne:(id)sender {
    
        static NSArray *colors;
        if (!colors) colors = @[[UIColor redColor], [UIColor greenColor], [UIColor yellowColor]];
    
        UIColor *currentColor = myLabel.backgroundColor;
        NSInteger index = [colors indexOfObject:currentColor];
        index = (index == NSNotFound || index == colors.count-1)? 0 : index+1;
    
        self.myLabel.backgroundColor = colors[index];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-29
      • 2019-09-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      相关资源
      最近更新 更多