当我获取子视图索引时出现问题
当我获取子视图的索引时出现问题。
int index = [[YourUIView subviews] indexOfObject:YourUIButton];
一些 UITextField 是使用 xib 文件创建的。当我打印它的值时,我得到了不真实的索引。 @property(非原子,保留)IBOutlet UITextField *distanceTextField;例如:索引等于 2147483647
但是当我以编程方式添加对象时,我得到了真正的索引。指数等于12。为什么?
There is a problem when i am getting index of subview.
int index = [[YourUIView subviews] indexOfObject:YourUIButton];
Some UITextField are created with xib file. When i printed its value i get unreal index.
@property (nonatomic, retain) IBOutlet UITextField *distanceTextField; for example: index is equal to 2147483647
But when i add object programmatically i get real index. index is equal to 12. Why ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次使用框架方法搜索某个索引(或范围)时,都会根据
NSNotFound
常量检查其结果。在你的平台上NSNotFound恰好是2147483647。这解决了虚幻索引的谜团。至于为什么找不到您的视图的问题 - 检查在您执行此搜索时每个出口是否实际上都指向某个视图。
更新
您可以通过调试器或通过记录来检查是否设置了插座
Each time you perform a search for some index (or range) with framework methods check its result against
NSNotFound
constant. On your platform NSNotFound is happened to be 2147483647. That solves the mystery of unreal index.As for question why your view is not found - check if every outlet actually points to some view at the moment you perform this search.
Update
You can check if outlet is set with debugger or by logging it
最有可能的是,索引是
NSNotFound
,与NSIntegerMax
或2147483647相同,告诉你没有找到对象的索引。尝试在 Interface Builder 中手动设置对象的索引并对其进行搜索。
Most likely, the index is
NSNotFound
, which is the same asNSIntegerMax
or 2147483647, telling you that the object's index wasn't found.Try manually setting the index of the object in Interface Builder and searching on that.