Pita
Pita

Reputation: 1474

UIImageView not setting image

Im trying to set a UIImageView from a segue, and for some reason the image is not getting set.. Heres the .h files of my class that subclassed a UIViewController

@interface PhotoDisplayViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *photoView;
-(void)setPhoto:(UIImage *)photo;
@end

and heres the setPhoto

-(void)setPhoto:(UIImage *)photo{
    NSLog(@"PHOTO %@", photo);
    _photoView.image = photo;
    NSLog(@"MYPHOTO %@", _photoView.image);
}

when i call setPhoto from prepare for segue, i see this in the console

2012-12-16 13:26:22.129 TestApp[2183:907] PHOTO <UIImage: 0x1fd7cd80>
2012-12-16 13:26:22.130 TestApp[2183:907] MYPHOTO (null)

Why is this happening?

Upvotes: 0

Views: 821

Answers (3)

Prasad G
Prasad G

Reputation: 6718

I think you called setPhoto: method as following. In this situation, first setPhoto: is called and after that viewDidLoad method is called in PhotoDisplayViewController. That's why photoView is nil. Actually viewDidLoad method should be called in PhotoDisplayViewController.So you should change the code slightly.

-(void)someActionMethod{
          [self performSegueWithIdentifier:yourIdentifier sender:nil];
 }
 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
            if ([[segue identifier] isEqualToString:yourIdentifier]) {
                photoDisplayVC = [segue destinationViewController];//photoDisplayVC is a object of         PhotoDisplayViewController
                 [photoDisplayVC setPhoto:imageObject];
 }

The change Code: In this situation first viewDidLoad method is called and after that setPhoto: method is called in PhotoDisplayViewController. Then you will get image.

-(void)someActionMethod{
  [self performSegueWithIdentifier:yourIdentifier sender:nil];
 [photoDisplayVC setPhoto:imageObject];
   }
  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
                if ([[segue identifier] isEqualToString:yourIdentifier]) {
                    photoDisplayVC = [segue destinationViewController];//photoDisplayVC is a object of         PhotoDisplayViewController

     }

I think it will be helpful to you.

Upvotes: 0

ipinak
ipinak

Reputation: 6029

I would advise you to change the weak to strong and take a look on this link.

Objective-C - weak property - getter autoreleases (Automatic Reference Counting)

Upvotes: 0

Cory Kilger
Cory Kilger

Reputation: 13044

It looks like _photoView is probably nil. It may not be set when loading the nib. Make sure you've wired it up properly in IB. Or perhaps you are calling -setPhoto: before the view has loaded.

Upvotes: 3

Related Questions