如何为Uibutton设置了哪个图像



在我的应用中,我有一个上下文,我使用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;
        }
}

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

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

我该怎么做可以帮助我,

预先感谢

您无法在按钮上获得当前设置的图像的名称,您所能获得的只是引用图像的内存地址。一个很好的替代方法是使用按钮标记属性来指定包含图像名称的数组中的索引。例如:

myArray = [[NSArray alloc] initWithObjects:@"something.png",@"somethingElse.png",@"etc.png", nil];
- (void)myMethod:(UIButton *)sender
{
    NSString *imageNameSelected = [myArray objectAtIndex:sender.tag];
}

因此,在此示例中,当您将按钮映像设置为" sothing.png"时,将按钮标记属性设置为" 0"或" 1",for" sosingselse.png"等...

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

最新更新