开发者

How to get the name of the backgroundImage from a button?

i´ve created different buttons and initalized them with an array with photos as backgroundImage.

UIButton* myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setBackgroundImage:[UIImage imageNamed: (NSString*) obj ] forState:UIControlStateNormal];
[myButton addTarget:nil action:@selecto开发者_如何学Pythonr(buttonDown: ) forControlEvents:UIControlEventTouchUpInside];

Here´s my buttonDown-method:

-(void) buttonDown:(UIButton*) sender {
NSLog(@"pressed: %@",   sender.currentBackgroundImage  );}

That traced me that: "pressed: < UIImage: 0x6625c40 >"

Now i want to know how can i get that image-name of any clicked button? Would be nice to know.Thanks for your time.


The way i tried to get the UIbutton's background imageName is , set the title of UIButton same that of image name and set the hidden property of button label to YES. (I was using iCarousel)

 button.titleLabel.text=[imageArray objectAtIndex:index];
button.titleLabel.hidden=YES;
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

and in buttonTapped,

UIButton *button=(UIButton*)sender;

NSLog(@"The button title is %@",sender.titleLabel.text);
 // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Button Tapped Is" message:sender.titleLabel.text delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
//[alert show];

woking fine.


Since UIImage does not store the name of the image it contains, you can't. You have to store the name elsewhere by yourself in relationship to the Button or image.

regards

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜