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 · Dec 20, 2015 · 3161 views
    This topic created in 3978 days ago, the information mentioned may be changed or developed.
    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 replies  •  2015-12-21 16:17:48 +08:00
    fogisland
    fogisland
    Dec 20, 2015❤️1
    1
    self.view.subviews 里除了 UIButton 外很可能还有很多其它类型的子 view ,比如 UILabel 什么的,不做过滤的话,会把不是 UIButton 的 view 强转为 UIButton , 显然不合理。
    推荐类似的问题去 segmentfault 或者 cocochina 问,那里 iOS 开发的多一些。
    jpchen
    jpchen
    OP
    Dec 20, 2015
    2
    @fogisland 谢谢,我新手,十分感谢
    UtopiaCHN
    UtopiaCHN
    Dec 20, 2015 via iPhone
    3
    @fogisland iv2ex
    superleexpert
    superleexpert
    Dec 21, 2015
    4
    另外,觉得这个逻辑可以完善下,这个 for 只能遍历到 self.view 下的所有控件,可以用个递归继续往下找, view 的 view 的 view 的。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3857 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:43 · PVG 13:43 · LAX 21:43 · JFK 00:43
    ♥ Do have faith in what you're doing.