开发者

There is a problem when i am getting index of subview

开发者 https://www.devze.com 2023-02-17 02:43 出处:网络
There is a problem when i am getting index of subview. int index = [[YourUIView subviews] indexOfObject:YourUIButton];

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 valu开发者_StackOverflowe 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 ?


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消