【问题标题】:There is a problem when i am getting index of subview获取子视图索引时出现问题
【发布时间】:2011-03-15 13:06:53
【问题描述】:

获取子视图索引时出现问题。

int index = [[YourUIView subviews] indexOfObject:YourUIButton];

一些 UITextField 是用 xib 文件创建的。当我打印它的值时,我得到了虚幻的索引。 @property(非原子,保留)IBOutlet UITextField *distanceTextField;例如:索引等于 2147483647

但是当我以编程方式添加对象时,我得到了真正的索引。 index 等于 12。为什么?

【问题讨论】:

    标签: iphone uiview xib


    【解决方案1】:

    每次您使用框架方法搜索某个索引(或范围)时,都会根据NSNotFound 常量检查其结果。在你的平台上,NSNotFound 恰好是 2147483647。这就解开了虚幻索引的谜团。

    int index = [parentView.subviews indexOfObject:importantButton];
    if (index != NSNotFound) {
      // found it
    } else {
      // no luck
    }
    

    至于为什么没有找到您的视图的问题 - 检查在您执行此搜索时每个出口是否确实指向某个视图。

    更新

    您可以使用调试器或记录它来检查插座是否设置了

        NSLog(@"myView - %@", myView);
    

    【讨论】:

    • 哦不,UITextField *distancetextField; @property(非原子,保留)IBOutlet UITextField *distanceTextField; - 拼写错误;哈哈 - 非常感谢你
    【解决方案2】:

    很可能,索引是NSNotFound,与NSIntegerMax 或2147483647 相同,告诉您找不到对象的索引。

    尝试在 Interface Builder 中手动设置对象的索引并对其进行搜索。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 2018-12-16
      相关资源
      最近更新 更多