我创建了不同的按钮,并使用一个以照片作为背景图像的数组来初始化它们。
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setBackgroundImage:[UIImage imageNamed: (NSString*) obj ] forState:UIControlStateNormal];
[myButton addTarget:nil action:@selector(buttonDown: ) forControlEvents:UIControlEventTouchUpInside];
这是我的buttonDown方法:
-(void) buttonDown:(UIButton*) sender {
NSLog(@"pressed: %@", sender.currentBackgroundImage );}
这追踪到了我:“按下:< UIImage: 0x6625c40 >”
现在我想知道如何获取任何单击按钮的图像名称? 很高兴知道。感谢您的宝贵时间。
最佳答案
我尝试获取 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];
以及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];
工作正常。
关于iphone - 如何从按钮获取背景图像的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3158737/