如何使用 Objective-C 更改多个 UIButton 颜色 [英] How to change multiple UIButton colors with Objective-C

查看:31
本文介绍了如何使用 Objective-C 更改多个 UIButton 颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了许多按钮,我想要一种有效的方式来改变所有的背景颜色,而不是一个一个地改变它或在 AppDelegate 上进行.

I have a number of buttons used and I want an efficient way of changing all it's background color instead of changing it one by one or doing it on the AppDelegate.

我基本上想避免这种情况

I basically want to avoid this

  buttonOne.backgroundColor = [UIColor BlueColor];
  buttonTwo.backgroundColor = [UIColor BlueColor];
  buttonThree.backgroundColor = [UIColor BlueColor];
  buttonFour.backgroundColor = [UIColor BlueColor];

不在 AppDelegate 中执行此操作的有效方法是什么.

What's an efficient way of doing this without doing it in the AppDelegate.

推荐答案

你可以这样做:

UIColor *color = [UIColor blueColor];
NSArray *buttons = @[buttonOne, buttonTwo, buttonThree, buttonFour];
for(UIButton *button in buttons) {
   button. backgroundColor = color;
}

或者,一种更酷的方式,使用 KVC:

Or, a cooler way, using KVC:

NSArray *buttons = @[buttonOne, buttonTwo, buttonThree, buttonFour];
[buttons setValue:[UIColor blueColor] forKey:@"backgroundColor"];

这篇关于如何使用 Objective-C 更改多个 UIButton 颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆