获取子视图索引时出现问题
Posted
技术标签:
【中文标题】获取子视图索引时出现问题【英文标题】: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。为什么?
【问题讨论】:
【参考方案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 中手动设置对象的索引并对其进行搜索。
【讨论】:
以上是关于获取子视图索引时出现问题的主要内容,如果未能解决你的问题,请参考以下文章
当我在真实设备中频繁推送/返回视图控制器时出现错误“索引超出范围” - Swift