如何从按钮获取backgroundImage的名称?

gef*_*rce 2 iphone xcode

我已经创建了不同的按钮,并使用带有照片作为backgroundImage的数组进行了初始化.

UIButton* myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setBackgroundImage:[UIImage imageNamed: (NSString*) obj ] forState:UIControlStateNormal];
[myButton addTarget:nil action:@selector(buttonDown: ) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

这是我的buttonDown方法:

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

这跟我说:"按下:<UIImage:0x6625c40>"

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

Sah*_*ngh 6

路上我试图让 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];
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)

好好的.


小智 5

由于UIImage不存储它包含的图像的名称,所以不能.您必须自己将名称存储在与Button或图像相关的其他位置.

问候