Kyle Greenlaw
Kyle Greenlaw

Reputation: 737

Change image in a UIImageView in a different class

I have two views, One is the Gameview and the other and a menu where I would like the user to select a button and it will change the image view in GameView to that image.

I has this bit of code for doing this but It doesn't seem to be working...

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqual:@"goToGameViewsSegue"]) {
        MainMenuView_4inch_White *dest = [segue destinationViewController];
        dest.imageView = CloudyBlue; //set the image in your view as the image in the settings view
        [dest.imageView setImage:[UIImage imageNamed:@"Cloudy-Eye-Blue.png"]];
        NSLog(@"Image should have been changed here.. How come it didn't?");

    }
}

I did check over all of the connection and if the segues was connected and it is all good so mustn't be that.

Upvotes: 0

Views: 469

Answers (1)

TotoroTotoro
TotoroTotoro

Reputation: 17612

Since you're changing the UIImageView object in the destination view controller, you need to make sure it's added as a subview to the correct superview.

 dest.imageView = CloudyBlue; 
 [dest.view addSubview:CloudyBlue]; // add it to the correct view and set the frame, depending on what you need.

But for the sake of keeping your code clean and modular, I recommend doing this kind of thing inside the destination view controller. Having one view controller mess with another's views is error prone and I don't recommend it.

For example, you could have this in your destination view controller:

- (void)setImage:(UIImage *)image 
{
     // 1) set up self.imageView, if necessary

     // 2) set the image
     self.imageView.image = image;     
}

Upvotes: 2

Related Questions