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
How to get the name of the backgroundImage from a button?
提问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 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
)
我试图让该方式 UIbutton
的背景imageName
中,设置title
的UIButton
相同映像名称是和设置hidden property
的button label
到YES
。(我正在使用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
问候