views:

839

answers:

1

Hi,

I'm trying to set the image of a new view. I set the controller's image an then display the view. But in the controller's viewDidLoad, no image is available.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

EditPictureSaveViewController *editPictureController = [EditPictureSaveViewController alloc];

[editPictureController initWithNibName:@"EditPictureSaveView" bundle:[NSBundle mainBundle]];

UIImageView *imageView = [ [ UIImageView alloc ] initWithImage: image ];
[imageView setImage:image];
[editPictureController setPreview:imageView];

[self.navigationController pushViewController:editPictureController animated:YES];
+1  A: 

That's because viewDidLoad is called by

[editPictureController initWithNibName:@"EditPictureSaveView" bundle:[NSBundle mainBundle]];

after the NIB is loaded.

editPictureController's image property (which I assume is a UIImageView - confusing!) is not set until after this.

Do whatever you're doing in viewDidLoad in the viewWillAppear: method instead - your image property will be set when pushViewController:animated: calls viewWillAppear:.

coob
Thank you! Now I store my image in the appDelegate and set it to the view in viewWillAppear:The UIImageView instead of UIImage was just for test purposes - forgot it to remove. ;-)
Stefan
While it might be fine for this application, be wary of using your AppDelegate as the fallback for storage of data, effectively turning it into a repository for global variables. Doing this not only weakens your MVC, but also will lead to toting objects around in memory far longer than they are needed.
mmc