iphone - 如何从按钮获取背景图像的名称?

标签 iphone xcode

我创建了不同的按钮,并使用一个以照片作为背景图像的数组来初始化它们。

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是 , 设置titleUIButton与图像名称相同并设置 hidden propertybutton labelYES 。 (我正在使用 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/

相关文章:

iphone - Xcode 中的共享类操作?

iphone - 正确填充后的空数组

iphone - 选择时 Xcode UITableView 行未突出显示

ios - 在 swift 中以编程方式创建 UIButton 后,如何在 Storyboard中链接 UIButton

ios - 钥匙串(keychain) - 安全数据存储

iphone - 如何从 iphone 应用程序将事件添加到 Google 日历?

ios - 着色器程序在 Cocos2d 2.2 Obj.C iOS 12 中无法运行

iphone - 在自定义键盘中添加表情符号

ios - ARKit : Show object with actual size

swift - 第一次从 firebase 检索图像后在本地保存图像(swift 4.2 )