开发者

Unable to assign images to an imageView from an Array, but can assign images to an imageView from a UIImage variable

I have a problem with arrays and passing images between views that I would like some help on! So the background is that I have:

• RootViewController (which handles my table view)

• CommunicationViewController which handles the detail of the selected element from the table

• SelectSlideViewController which displays an image clicked on from the CommunicationViewController and allows the user to select a different one from the camera roll

So the problem:

• In the CommunicationViewConroller, I have the following code if the user clicks on a button:

- (IBAction) selectSlide:(id) sender
{
 if(self.selectSlideView == nil)
 {
 SelectSlideViewController *viewController = [[SelectSlideViewController alloc]
           initWithNibName:@"SelectSlideViewController" bundle:[NSBundle mainBundle]];
  self.selectSlideView = viewController;
  [viewController release];
 }

 [self.navigationController pushViewController:self.selectSlideView animated:YES];
 self.selectSlideView.cmn = cmn;
 self.selectSlideView.title = cmn.name;
 self.selectSlideView.imageView.image = self.myImage5;
}

And the above code works, if for, example, I click on button 5, as it sets image5 to the view in the “SelectSlideViewController”.

However, I would like to have multiple buttons using the same “selectSlide” action – and to do that, I need to figure out which button was pressed, and then assign the correct image to the “SelectSlideViewController” from an array of images (or a series of if-else statements which is clunky).

• So my revised code is as follows, but it doesn’t work with the array – any thoughts?:

- (IBAction) selectSlide:(id) sender
{
 if(self.selectSlideView == nil)
 {
 SelectSlideViewController *viewController = [[SelectSlideViewController alloc]
             initWithNibName:@"SelectSlideViewController" bundle:[NSBundle mainBundle]];
  self.selectSlideView = viewController;
  [viewController release];
 }

 NSUInteger tmpInt = -1;
 tmpInt = [buttonArray indexOfObject:sender];

 [self.navigationController pushViewController:self.selectSlideView animated:YES];
 self.selectSlideView.cmn = cmn;
 self.selectSlideView.title = cmn.name;

 NSLog(@"The int was %d",tmpInt);
 NSLog(@"This is the image array size %d ",[imageArray count]); 

 If(tmpInt >-1 && tmpInt <9)
 {
  self.selectSlideView.imageView.image = [imageArray objectAtIndex:tmpInt];
 }
 /** this code works, but is a bit clunky:
 if(tmpInt == 0)
  self.selectSlideView.imageView.image = self.myImage1;
 else if开发者_开发问答 (tmpInt == 1)
  self.selectSlideView.imageView.image = self.myImage2;
 else if (tmpInt == 2)
  self.selectSlideView.imageView.image = self.myImage3;
 else if (tmpInt == 3)
  self.selectSlideView.imageView.image = self.myImage4;
 else if (tmpInt == 4)
  self.selectSlideView.imageView.image = self.myImage5;
 else if (tmpInt == 5)
  self.selectSlideView.imageView.image = self.myImage6;
 else if (tmpInt == 6)
  self.selectSlideView.imageView.image = self.myImage7;
 else if (tmpInt == 7)
  self.selectSlideView.imageView.image = self.myImage8;
 else if (tmpInt == 8)
  self.selectSlideView.imageView.image = self.myImage9; 
 **/
}

I have definitely confirmed that the imageArray is of size 9, and that it is picking the correct index from the array, however the SelectSlideViewController doesn't display the image, if accessed from the array. It displays it if accessed using the clunky if-else statements.


You could try to load the images directly the first time, then cache afterwards. (I've excluded the caching code, and the scaling code, but you can add them after you get the image).

   If(tmpInt >-1 && tmpInt <9)
     {
      // Check if already cached
         self.selectSlideView.imageView.image = [self getButtonImages:tmpInt];
      // add scaling code here for smaller image

     }

In @implementation you can add:

 - (UIImage) getButtonImages:(NSInteger)index
    {   
        UIImage* myImage = [UIImage imageNamed:@"image%d.jpg", index];
        return myImage;
    }


Here is the solution to the above problem (credited to "JSD" from www.iphonedevsdk.com):

Since selectSlideView.imageView was loaded from a nib the view wasn't loaded the first time through.

I had to include the following line of code before I assigned images from my Array to the view:

[selectSlideView view];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜