这是一个创建于 3101 天前的主题,其中的信息可能已经有所发展或是发生改变。
for (id obj in self.view.subviews) {
if ([obj isKindOfClass:[UIButton Class]]) {
UIButton* theButton = (UIButton*)obj;
theButton.xxx = ooo;
}
}
为什么在控件上遍历子控件时我们要在 for in 中取 id 属性再做 if ([obj isKindOfClass:[UIButton Class]])判断而不能直解取 uibutton ,像下面这样:
for (id obj in self.view.subviews) {
UIButton* theButton = (UIButton*)obj;
theButton.xxx = ooo;
}
4 条回复 • 2015-12-21 16:17:48 +08:00
![fogisland](https://cdn.v2ex.com/gravatar/aea5b7052d74a9384fb81ff5bc764150?s=48&d=retro) |
|
1
fogisland 2015-12-20 00:23:25 +08:00 1
self.view.subviews 里除了 UIButton 外很可能还有很多其它类型的子 view ,比如 UILabel 什么的,不做过滤的话,会把不是 UIButton 的 view 强转为 UIButton , 显然不合理。 推荐类似的问题去 segmentfault 或者 cocochina 问,那里 iOS 开发的多一些。
|
![jpchen](https://cdn.v2ex.com/gravatar/8557c57108a818f47ae762877abaf9be?s=48&d=retro) |
|
2
jpchen 2015-12-20 00:30:36 +08:00
|
![UtopiaCHN](https://cdn.v2ex.com/avatar/22fd/a4de/90666_normal.png?m=1481764534) |
|
3
UtopiaCHN 2015-12-20 02:25:54 +08:00 via iPhone
|
![superleexpert](https://cdn.v2ex.com/avatar/a399/11c2/16959_normal.png?m=1491456978) |
|
4
superleexpert 2015-12-21 16:17:48 +08:00
另外,觉得这个逻辑可以完善下,这个 for 只能遍历到 self.view 下的所有控件,可以用个递归继续往下找, view 的 view 的 view 的。。。
|