iphone - 我可以编写一个通用函数来修改所有 View Controller 的 UI 元素吗?

标签 iphone objective-c ios ios4

我想知道是否有一种方法可以编写一个通用函数,该函数可以存储在一个可以修改 UI 元素的 Utility 类中?

换句话说,现在我正在以编程方式更改某些 UI 元素的颜色,如下所示:

headerLabel.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];

//Where red, green and blue are variables that are derived from another function 
//that converts Hex to RGB and 'headerLabel' is a UILabel with a connected IBOutlet

每个 View Controller 都会发生同样的过程,所以我想知道是否有办法让它成为一个通用函数?

在 Java/Android SDK 中,我可以传递类的上下文并从那里完成其余的工作。

先感谢您!!

编辑:

稍微重新表述一下这个问题,我想要 headerLabel.textColor=函数和其他方法,如 headerLabel.text = @"foo"在那个通用类中,不一定只是颜色函数。

最佳答案

如果您想在所有文件中包含 HEX->UICOLOR,请输入:

#define UIColorFromHEX(hexValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:1.0]

在您的 Prefix.pch文件。然后,在每个文件中,您可以通过
something.color = UIColorFromHEX(0xCCCCCC);

您可以通过 #define 定义所有内容宏。如果要定义 headerLabel.textColor , 采用
#define setHeaderLabelColor headerLabel.textColor = [UIColor ...]

关于iphone - 我可以编写一个通用函数来修改所有 View Controller 的 UI 元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7125209/

相关文章:

ios - NSURLSession 在 iOS 7.0.x 上挂起

iphone - NSIndexPath 在 iOS5 上崩溃

iPhone - 如何处理多个 UIImagePickerControllerDelegate?

IOS & Objective C - 如何用国家列表填充 UIPickerView?

android - 如何在 Flutter 中使用本地资源播放音频?

ios - 杀死翻转开关中的应用程序

ios - 逐帧解码 h264 示例 ios 7

ios - 静态表格单元格左侧的图像或 View

ios - 在函数中将 Date 转换为 DateComponents 以在 Swift 3 中安排本地通知

iphone:从 sqlite 数据库中删除行