没有重新加载imageview

时间:2013-07-06 10:41:07

标签: iphone ios objective-c

我有一个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 个答案:

答案 0 :(得分:3)

我正在寻找你的代码这段代码是完美的,但我认为问题是由于图像不存在于捆绑中,如果图像存在于捆绑中然后删除所有图像并再次添加尝试这可能对你有用。 / p>

答案 1 :(得分: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。

EDITE:

将它用作(只需举例说明你需要在代码中更新它)

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

  self.myImgView.image = [UIImage imageNamed: images[sender.tag]];
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

请尝试以下方法。

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