Some easily overlooked points about UIButton
来源:互联网 发布:淘宝代销货 编辑:程序博客网 时间:2024/05/16 06:34
在UIButton中我们可以设置图片、也可以设置文字。这是因为在它里面其实还包括了一个UIImageView 和一个UIlabel在XCode,进入UIButton的头文件:
这里需要注意的是 UILabel * titleLabel 以及 UIImageView * imageView 都是ReadOnly,两个成员变量是不能通过点语法赋值的。
有的时候,我们想换个跟往常不一样的写法,比如可能写成下面的代码:
虽然在编译的时候不会报出错误,但是实际上,button是取不到这张图片的,因为imageView是只读的,它是没有set方法的。
同样的,下面修改title的方法也是错误的:
所以,在给button设置图片、title文字的时候我们都会选择:
在高亮或者其他button状态下,系统会自动调节,与normal保持一致
这里有一点需要注意的是:
在修改button的title值后,通过button.titleLabel.text返回的字符串长度是不准确的。例如上方,我们通过button.titleLabel.text分别打印出了
未设置title
设置title后
修改title后
三种状态下返回的字符串长度:发现值只能增加,即使在最后我们将title设定成了nil,它打印的长度仍然是2
所以,当我们需要动态判断UIButton中title的文字长度时,我们会选择
[button titleForState:(UIControlState)],因为它返回给我们的总是最新状态下title的字符串。
0 0
- Some easily overlooked points about UIButton
- Some points about modern Dojo
- some points to be think about how to create your
- Some points about postback and multi-language drop down list
- some points
- Some words about ICE...
- About some techno-words
- some links about UML
- Some tutorial about geometry
- Some videos about Avalon
- Some Tips about Interbase
- some understanding about Java
- Some words about Computer
- some function about TIVOLI
- some about wcg
- some note about js
- some thing about JSon
- some words about "route"
- Ubuntu 14.04下安装Golang以及LiteIDE
- 百度地图 Android 定位SDK 开发指南4.2
- 原版windows下载地址
- Hive 的 CSV Serde介绍和使用
- JSON 之FastJson解析
- Some easily overlooked points about UIButton
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- EventBus 学习资源
- POJ 2259 Team Queue
- 迷宫求解(栈)
- 学习讲究细嚼慢咽?
- 如何做一个简单的开放接口(3)-核心引擎(下)
- 互联网思维-产品思维(2)
- 使用struts2的标签报错No result defined for action and result input