2

在我看来,我有 1 个UIimageview和 4 个UIButton,我展示了来自NSMutableArray. 如何在UIButton没有图像视图图像名称的情况下将标题显示为.png?我尝试使用以下编码

imageary=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"dear.jpg"],[UIImage imageNamed:@"donkey.jpg"],[UIImage imageNamed:@"elephant.jpg"],[UIImage imageNamed:@"fox.jpg"],[UIImage imageNamed:@"giraffe.jpg"],[UIImage imageNamed:@"goat.jpg"],[UIImage imageNamed:@"buffallo.jpg"],[UIImage imageNamed:@"bull.jpg"],[UIImage imageNamed:@"cow.jpg"],[UIImage imageNamed:@"crocodile.jpg"], nil];


 - (NSString *) convertToDisplayName:(NSString *)actual
{
return [actual stringByReplacingOccurrencesOfString:@".png" withString:@" "];

}

尝试使用上述方法检索名称时

int i=0;
for(UIView *view in self.view.subviews)
{
    if([view isKindOfClass:[UIButton class]])
    {
       mybutton = (UIButton *)view;
        if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4)
        {

             animage.image=[imageary objectAtIndex:i];
            name=[self convertToDisplayName:[imageary objectAtIndex:i]];
            [mybutton setTitle:name forState:UIControlStateNormal];
            NSLog(@"current image name :%@",name);
            i++;

        }
    }
   }

它显示错误

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x73f8eb0

请帮助我必须纠正的地方。

4

4 回答 4

3

您在该函数中传递 UIImage 对象,因为您应该维护图像名称数组并在此函数中传递该图像名称,并且您可以通过使用此方法删除扩展名来简单地获取名称:

[actual stringByDeletingPathExtension];

这是你应该做的

imageary=[[NSMutableArray alloc]initWithObjects:@"dear.jpg",@"donkey.jpg",@"elephant.jpg",@"fox.jpg",@"giraffe.jpg",@"goat.jpg",@"buffallo.jpg",@"bull.jpg",@"cow.jpg",@"crocodile.jpg", nil]; // declare array of names not images
 - (NSString *) convertToDisplayName:(NSString *)actual
{
  return [actual stringByDeletingPathExtension]; //return image name without extension
}

比这个

int i=0;
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UIButton class]])
{
   mybutton = (UIButton *)view;
    if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4)
    {
         i = rand() % [imageary count]; // set random image
         animage.image=[UIImage imageNamed:[imageary objectAtIndex:i]]; // return image with this name
        name=[self convertToDisplayName:[imageary objectAtIndex:i]];
        [mybutton setTitle:name forState:UIControlStateNormal];
        NSLog(@"current image name :%@",name);
        i++;

    }
}
}

希望有帮助!

于 2012-11-08T04:47:21.573 回答
0

在这行代码中:

name=[self convertToDisplayName:[imageary objectAtIndex:i]];

您正在将UIImage对象传递给需要对象的方法NSString

您需要传递文件名,而不是图像。

作为旁注 - 这一行:

return [actual stringByReplacingOccurrencesOfString:@".png" withString:@" "];

应该:

return [actual stringByReplacingOccurrencesOfString:@".png" withString:@""];
于 2012-11-08T04:39:48.570 回答
0

看起来

[图像对象AtIndex:我]

是一个

UIImage

目的。它不是 NSString。所以你得到了例外。

您可以使用图像名称制作图像,您可以这样做

animage.image= [UIImage imageNamed:[imageary objectAtIndex: i ]];

于 2012-11-08T05:24:12.077 回答
0

您可以使用: NSArray *arr = [[imageary objectAtIndex:indexNo] componentsSeparatedByString:@"."]; Nsstring *imageName = [arr objectAtIndex:0];

你可以使用 imageName 来设置你的 UIButton 标题

于 2012-11-08T07:42:39.903 回答