【问题标题】:How to get which image has been set for the UIButton如何获取已为 UIButton 设置的图像
【发布时间】:2012-10-19 18:39:22
【问题描述】:

在我的应用程序中,我有一个上下文,我正在使用 NSTimer 更改 UIButton 的图像,如下所示

 [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(LoadImageToTheAdButton) userInfo:nil repeats:YES];


-(void)LoadImageToTheAdButton
{
     NSString *strPath = [arrPaths objectAtIndex:0];
        strPath = [strPath stringByAppendingPathComponent:@"Folder"];
        strPath = [strPath stringByAppendingPathComponent:[arrAdImgNames objectAtIndex:intAdImgIndex]];
        UIImage *imgForAdBtn = [[UIImage alloc]initWithContentsOfFile:strPath];
        [btnAd setBackgroundImage:imgForAdBtn forState:UIControlStateNormal];

        if(intAdImgIndex < [arrAdImgNames count]-1)
        {
            intAdImgIndex++;
        }
        else
        {
            intAdImgIndex = 0;
        }
}

现在点击按钮 *我需要知道按钮上设置了哪个图像我的意思是,我需要图像名称 *

我需要它,因为我正在根据该图像加载一些链接,

请问哪位大侠能帮帮我,怎么办,

提前致谢

【问题讨论】:

  • 您不能将名称保存在视图控制器的属性中(同时更新它们)吗?

标签: iphone objective-c ios xcode uibutton


【解决方案1】:

无法获取按钮上当前设置的图像名称,只能获取引用该图像的内存地址。一个不错的选择是使用按钮标签属性来指定包含图像名称的数组中的索引。例如:

myArray = [[NSArray alloc] initWithObjects:@"something.png",@"somethingElse.png",@"etc.png", nil];

- (void)myMethod:(UIButton *)sender
{
    NSString *imageNameSelected = [myArray objectAtIndex:sender.tag];
}

因此,对于本示例,当您将按钮图像设置为“something.png”时,您会将按钮标签属性设置为“0”,或者将“somethingElse.png”设置为“1”等...

【讨论】:

    【解决方案2】:

    在任何视图中尝试 setAccessibility* 属性。这些非常有帮助。我通过在我的应用程序中使用此属性来获取图像名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 2022-01-14
      • 2021-11-17
      相关资源
      最近更新 更多