开发者

I just had added UIButton to UIView. How can I get buttons index?

I just had added UIButton to UIView. How can I get开发者_如何学编程 buttons index? Thank you , solved.

i have a problem. Some UIButtons 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 ?


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


Looping through a UIView's subviews is an unreliable way to find a button. There is no guarantee as to the position of the button in the subview array, especially if the system (or you) adds other elements to the view later on. Without some kind of checking, you can easily crash your app if you retrieve the wrong subview and try to operate on it.

A better way is set a custom tag value to the UIButton.

e.g.

button.tag = 1000;

Now you can find the button directly through

[view viewWithTag:1000];

This can be done for any UIView subclass, not just UIButtons.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜