xcode 如何从按钮获取背景图片的名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3158737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:30:19  来源:igfitidea点击:

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

iphonexcode

提问by geforce

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:@selector(buttonDown: ) forControlEvents:UIControlEventTouchUpInside];

Here′s my buttonDown-method:

这是我的 buttonDown 方法:

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

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

这让我知道:“按下:< 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.

现在我想知道如何获得任何点击按钮的图像名称?很高兴知道。谢谢您的时间。

回答by Saheb Singh

The way i tried to get the UIbutton's background imageNameis , set the titleof UIButtonsame that of image name and set the hidden propertyof button labelto YES. (I was using iCarousel)

我试图让该方式 UIbutton的背景imageName中,设置titleUIButton相同映像名称是和设置hidden propertybutton labelYES。(我正在使用iCarousel

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

and in buttonTapped,

并且在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.

很好。

回答by HerrVoennchen

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.

因为 UIImage 不存储它包含的图像的名称,所以你不能。您必须自己将名称存储在与按钮或图像相关的其他地方。

regards

问候