我正在尝试从自定义按钮获取背景图片的名称。
点击UIButton时调用此方法:
-(void)getButtonImageName:(id)sender{
UIButton *resultButton = (UIButton *)sender;
NSLog(@" The button's image is %@.", resultButton.currentImage);
}
我可以从按钮获取标题和其他属性,但不能获取背景图像名称。
任何人都知道该怎么做?谢谢!
答案 0 :(得分:10)
这是获取自定义按钮的当前图像的最简单方法
-(void)yesButtonButtonCliked:(id)sender
{
UIButton *tmpYes = (UIButton *)sender;
[self ClearAllButtons:tmpYes.tag:@"YES"];
UIImage* selectedImg=[UIImage imageNamed:@"checkboxtick.png"];
if (tmpYes.currentImage == selectedImg )
{
}
else
{
[tmpYes setImage:[UIImage imageNamed:@"checkboxtick.png"] forState:UIControlStateNormal];
}
NSLog(@"Button tag is %d",tmpYes.tag);
}
答案 1 :(得分:1)
没有方法可以将UIImage转换为[UIImage imageNamed:]使用的名称。
您是否滥用图片存储数据? 您尝试使用图像名称完成什么?
编辑:
我有几个按钮,我想在按下按钮时获取图像名称,切换到另一个视图控制器并使用名称加载该图像。
无需执行此操作,只需传递UIImage实例即可 如果要根据名称显示不同的图像,就像您有缩略图并想要在另一个视图控制器中显示大版本,您必须从数据模型中获取图像名称并将其传递给视图控制器。 /> 你如何决定哪个按钮获取哪个图像?
答案 2 :(得分:0)
@fluchtpunkt正如你所说,我有一些带有背景图像的UI按钮的缩略图。会发生的是,我正在为前一个视图控制器中选择的类别创建如下内容。像这样......
if( [strSection isEqualToString:@"animals"] ){
[button_thumbnail_1 setBackgroundImage:[UIImage imageNamed:@"image1.png"] forState:(UIControlState)normal];
[button_thumbnail_2 setBackgroundImage:[UIImage imageNamed:@"image2.png"] forState:(UIControlState)normal];
}
if( [strSection isEqualToString:@"cars"] ){
[button_thumbnail_1 setBackgroundImage:[UIImage imageNamed:@"image1.png"] forState:(UIControlState)normal];
[button_thumbnail_2 setBackgroundImage:[UIImage imageNamed:@"image2.png"] forState:(UIControlState)normal];
}
}
感谢您的回复
答案 3 :(得分:0)
您可以继承UIButton:
//.h
@interface MySpecialButton : UIButton {
int mySpecialAttribute;
}
@property (nonatomic) int mySpecialAttribute;
- (void) setBackgroundImage;
- (UIImage*) getImage;
@end
并使用您的特殊属性来设置和返回图像名称:
//.m
@implementation LetterButton
@synthesize mySpecialAttribute;
- (void) setBackgroundImage {
[self setBackgroundImage:[self getImage] forState:UIControlStateNormal];
[self setBackgroundImage:[self getImage] forState:UIControlStateDisabled];
}
- (UIImage*) getImage {
NSString *fileName = [NSString stringWithFormat:
@"My_special_image_%i.png", mySpecialAttribute];
return [UIImage imageNamed:fileName];
}
@end
在xib中设置按钮的类:
答案 4 :(得分:0)
关于Matthias的回答“不需要这样做,只需传递UIImage实例”,这是我的解决方案,其中包含新手的特定代码:
Calling .m:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//Create an instance of the destination controller
dmImageZoom *dmz = (dmImageZoom *)[segue destinationViewController];
//Get the tapped button (the image was in a button) into a button object
UIButton *btnImg= sender;
//Get the button's image object
UIImage *img=btnImg.currentImage;
//Pass the image object to the destination controller's instance variable
dmz.imgPassed=img;
Called .h:
@interface dmImageZoom : UIViewController {
//Instance variable
UIImage *imgPassed;
}
//Property for the object that presents the image
@property (strong, nonatomic) IBOutlet UIImageView *imgView;
//Property that enables it to be assigned a value
@property (strong, nonatomic) IBOutlet UIImage *imgPassed;
Called .m:
- (void)viewDidLoad
{
...
imgView.contentMode = UIViewContentModeScaleAspectFit;
//Assign the image to view object
imgView.image=imgPassed;
答案 5 :(得分:0)
UIButton *senderButton = (UIButton*)sender;
if ([senderButton.currentTitle isEqualToString:@"Hello"]) {
[senderButton setTitle:@"H" forState:UIControlStateNormal];
}
else {
[senderButton setTitle:@"Hello" forState:UIControlStateNormal];
forState:UIControlStateNormal];
}
答案 6 :(得分:0)
@objc func btnWishListAddRemove(_ sender:UIButton){
let index = sender.tag
let product_id = array_packageList[index].product_id!
if sender.currentImage == UIImage(named: "wish list"){
sender.setImage(UIImage(imageLiteralResourceName: "wishlistfill"), for: .normal)
}
else if sender.currentImage == UIImage(named: "wish list fill"){
sender.setImage(UIImage(imageLiteralResourceName: "wishlist"), for: .normal)
}
}