【问题标题】:How can I identify view objects in a UIScrollView?如何识别 UIScrollView 中的视图对象?
【发布时间】:2014-02-28 16:31:06
【问题描述】:

我有一个 UIScrollView 有很多对象,我想识别它们中的每一个。我目前正在执行以下操作:

[learnLabel setTag:currentScrollElements];    
[learnStartButton setTag:currentScrollElements+10000];
[learnLike1Button setTag:currentScrollElements+20000];
[learnLike3Button setTag:currentScrollElements+30000];
[learnCommentField setTag:currentScrollElements+40000];

等等

理想的解决方案是一个系统,我可以将相同的数字两次分配给两个不同的对象(例如,分配给 UIButtonUILabel)。我不想用完空间,因为标签的最大值与整数的最大值相同。我该怎么做?

【问题讨论】:

  • 在您用完整数值之前,您的应用将因内存使用 LONG 而崩溃。
  • NSIntegerMax 是 2147483647。如果您有 20 亿的浏览量,那说明您除了识别它们之外还有其他问题...

标签: ios uiscrollview tags


【解决方案1】:

也许您可以将所有对象存储在 NSDictionary 中。

【讨论】:

    【解决方案2】:

    你可以这样做。

    #define LearnLabel          @"LearnLabel"
    #define LearnStartButton    @"LearnStartButton"
    #define LearnLike1Button    @"LearnLike1Button"
    #define LearnLike3Button    @"LearnLike3Button"
    #define LearnCommentField   @"LearnCommentField"
    ...
    ...
    ...
    
    
    NSMutableDictionary* childObjectsDictionary = [[NSMutableDictionary alloc] init];
    [childObjectsDictionary setObject:learnLabel forKey:LearnLabel];
    [childObjectsDictionary setObject:learnStartButton forKey:LearnStartButton];
    [childObjectsDictionary setObject:learnLike1Button forKey:LearnLike1Button];
    [childObjectsDictionary setObject:learnLike3Button forKey:LearnLike3Button];
    [childObjectsDictionary setObject:learnCommentField forKey:LearnCommentField];
    ...
    ...
    ...
    

    【讨论】:

      【解决方案3】:

      理论上,您可以毫无问题地使用标签,因为它可以用于比设备可能的最大容量更多的视图。

      但是在处理大量不同的视图之后,完全令人困惑难以管理。

      最好的办法是使用数组或字典,或者更好的是,重新设计视图结构并使用内置更好的内存管理支持的 UITableViewUICollectionView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-19
        • 2012-12-12
        • 1970-01-01
        • 1970-01-01
        • 2011-04-08
        • 1970-01-01
        • 1970-01-01
        • 2017-04-21
        相关资源
        最近更新 更多