2

私の見解では 1UIimageviewと 4UIButtonがあり、 の画像を表示しますNSMutableArrayUIButtonタイトルを画像ビューの画像名なしで表示するにはどうすればよい.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

それが思われるよう

[imageary objectAtIndex:i]

UIImage

物体。NSStringではありません。だからあなたは例外を得ました。

あなたは画像の名前で画像を作ることができますそしてあなたはこのようにすることができます

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

于 2012-11-08T05:24:12.077 に答える
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

以下を使用できます。 NSArray *arr = [[imageary objectAtIndex:indexNo] componentsSeparatedByString:@"."]; Nsstring *imageName = [arr objectAtIndex:0];

imageName を使用して UIButton のタイトルを設定できます

于 2012-11-08T07:42:39.903 に答える