当我获取子视图索引时出现问题 [英] There is a problem when i am getting index of subview

查看:60
本文介绍了当我获取子视图索引时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There is a problem when i am getting index of subview.

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

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

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

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

But when i add object programmatically i get real index. index is equal to 12. Why ?

推荐答案

每次使用框架方法搜索某个索引(或范围)时,都要根据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.

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

关于为什么找不到您的视图的问题-请在执行搜索时检查每个插座是否实际上都指向某个视图.

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.

更新

您可以检查插座是使用调试器设置还是通过登录设置

You can check if outlet is set with debugger or by logging it

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

这篇关于当我获取子视图索引时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆