2

我有 20 个按钮,每个按钮单击的每个按钮都会根据其标签值加载该数组图像。我的问题是只显示第一个按钮图像为什么我在下面的代码中尝试了这个。

    images=[[NSMutableArray alloc]init];


        [images insertObject:@"circle.png" atIndex:0];
        [images insertObject:@"cone.png" atIndex:1];
        [images insertObject:@"cube.png" atIndex:2];
        [images insertObject:@"cuboid.png" atIndex:3];
        [images insertObject:@"cylinder.png" atIndex:4];
        [images insertObject:@"ecplise.png" atIndex:5];
        [images insertObject:@"ellipsoid.png" atIndex:6];
        [images insertObject:@"frustrum of cone.png" atIndex:7];
        [images insertObject:@"kite.png" atIndex:8];
        [images insertObject:@"parallelepiped.png" atIndex:9];
        [images insertObject:@"parallelogram.png" atIndex:10];
        [images insertObject:@"polygon.png" atIndex:11];
        [images insertObject:@"rectangle.png" atIndex:12];
        [images insertObject:@"rectangula prizm.png" atIndex:13];
        [images insertObject:@"rhoumbus.png" atIndex:14];
        [images insertObject:@"sector.png" atIndex:15];
        [images insertObject:@"sphere.png" atIndex:16];
        [images insertObject:@"square.png" atIndex:17];
        [images insertObject:@"tapezoid.png" atIndex:18];
        [images insertObject:@"tourus.png" atIndex:19];
        [images insertObject:@"traingle.png" atIndex:20];


NSString * str=value2;


    NSLog(@"%@",str);

    //AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];




    NSInteger myInt = [str intValue];


    NSString *  imagename=[images objectAtIndex:myInt];

    NSLog(@"%@",imagename);


    self.imgview.userInteractionEnabled=YES;

    [imgview setImage:[UIImage imageNamed:imagename]];

    [imgview setNeedsDisplay];

    [imgview reloadInputViews];

str 包含按钮的标记值正确获取所有值。但仅显示第一张图像。

4

4 回答 4

3

我正在查看您的代码,此代码是完美的,但我认为问题是由于图像不存在于捆绑包中,如果图像存在于捆绑包中,则删除所有图像并再次添加尝试这可能对您有用。

于 2013-07-08T05:29:32.017 回答
0

我给你简单的代码,如何通过相关的按钮单击从 ImageArray 获取图像:

假设你有按钮的方法是

-(void)buttonTapped:(UIButton *)sender
{
   images=[[NSMutableArray alloc]init];
   [images addObject:[UIImage imageNamed:@"myImage1.png"]];
   .
   .
   .

   UIImage *image = (UIImage *)[image objectAtIndex:sender.tag]; /// i Hope your each button has its own Tag.  

   /// here you get relavent image from button tapped by its tag;

  // put this image of you imageView.

   self.myImgView = [[UIImageView alloc] initWithFrame:CGRectMake("AS YOU NEED")];
   self.myImgView.image = image;
   [self.mainBG addSubview:self.myImgView];
}

在这里确保你的按钮有标签 o 到 20。

编辑:

将其用作(只需举个例子,您需要在代码中按原样更新它)

-(IBAction)buttonTapped:(id)sender 
{
      NSString *images[] = 
    {
       @"image0.png",
       @"image1.png",
       .
       .
       .
    };

  self.myImgView.image = [UIImage imageNamed: images[sender.tag]];
}
于 2013-07-06T10:52:38.720 回答
0

您是否使用自定义图像视图,如果是,那么 [imgview setNeedsDisplay] 将起作用,否则它将不起作用。请也检查一下。

于 2013-07-08T05:50:04.013 回答
0

请尝试以下。

1)尝试self.myImgView.image = [UIImage imageNamed:@"cube.png"]使用其他图像,如@“rectangula prizm.png”,@“tourus.png”......看看如果你直接设置图像会发生什么。如果图像显示不正确,则图像不在捆绑包内或图像名称错误。

于 2013-07-08T06:08:17.590 回答