There is a problem when i am getting index of subview

一笑奈何 提交于 2019-12-23 01:35:16

问题


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 ?


回答1:


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.

Update

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

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



回答2:


Most likely, the index is NSNotFound, which is the same as NSIntegerMax 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.



来源:https://stackoverflow.com/questions/5312153/there-is-a-problem-when-i-am-getting-index-of-subview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!