开发者

xcode iphone Compare UIButton from :(id)sender with UIButtons from Array

开发者 https://www.devze.com 2023-02-27 09:36 出处:网络
I have several views with buttons on it. All those views are linked to one ViewController and in this ViewController I have IBOutlets defined for all those 50 buttons.

I have several views with buttons on it. All those views are linked to one ViewController and in this ViewController I have IBOutlets defined for all those 50 buttons.

To easy access them, I stored all UIButtons in a multidimensional NSMutableArray. For testing purpose I have given all buttons a unique titleLabel. The UIButtons are correctly stored in the Array, I can test that using the titleLabel.

开发者_开发技巧

After one button is pushed, I call a method:

-(IBAction)pushButton:(id)sender

In there I compare:

if ( [allButtonsArray objectAtIndex:1] == sender ) 
// do something

Allthough the selected UIButton from the Array and the sender Button have the same titleLabel (which is unique in my case) the IF statement is not true.

Does anybode have an idea why?

Thanks, Pat


have you tried casting them as UIButtons?

if ( (UIButton *)[allButtonsArray objectAtIndex:1] == (UIButton *)sender )

or use isEqual method instead of ==

also, why not just compare titleLabel instead?


I just output the UIButton using NSLog %@ which I am comparing and found out, that the information like "Frame", "opaque" "CALayer" etc is the same, BUT:

The real memory ID like "UIRoundedRectButton: =x4b3f7f0" is different Therefore the IF statement is never true.

What I did now is to assign each UIButton in the Interface-Builder a specific tag-value (in the View section). This tag can be accessed by [sender tag] and can be easily compared with a numeric array to check which button was pressed.

0

精彩评论

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