V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
jpchen
V2EX  ›  iDev

关于遍历 View 上所有的 UIButton 的问题,求问

  •  
  •   jpchen · 2015-12-20 00:16:55 +08:00 · 2351 次点击
    这是一个创建于 3061 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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
        1
    fogisland  
       2015-12-20 00:23:25 +08:00   ❤️ 1
    self.view.subviews 里除了 UIButton 外很可能还有很多其它类型的子 view ,比如 UILabel 什么的,不做过滤的话,会把不是 UIButton 的 view 强转为 UIButton , 显然不合理。
    推荐类似的问题去 segmentfault 或者 cocochina 问,那里 iOS 开发的多一些。
    jpchen
        2
    jpchen  
    OP
       2015-12-20 00:30:36 +08:00
    @fogisland 谢谢,我新手,十分感谢
    UtopiaCHN
        3
    UtopiaCHN  
       2015-12-20 02:25:54 +08:00 via iPhone
    @fogisland iv2ex
    superleexpert
        4
    superleexpert  
       2015-12-21 16:17:48 +08:00
    另外,觉得这个逻辑可以完善下,这个 for 只能遍历到 self.view 下的所有控件,可以用个递归继续往下找, view 的 view 的 view 的。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1094 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:34 · PVG 02:34 · LAX 11:34 · JFK 14:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.