Reputation: 3495
I got a UIViewController called SecondViewController which loads the photo library using UIImagePickerController. When I select a photo, it should present another view controller called PhotoViewController. To pass the image from SecondViewController to PhotoViewController I use a singleton. Here is the code:
SecondViewController:
@property (strong,nonatomic) UIImagePickerController *libraryPicker;
@property (strong,nonatomic) UIImage *chosenImage;
@property (strong,nonatomic) PhotoViewController *controller;
- (void)viewDidLoad
{
controller = [PhotoViewController sharedInstance];
libraryPicker = [[UIImagePickerController alloc] init];
libraryPicker.delegate = self;
libraryPicker.allowsEditing = NO;
libraryPicker.navigationBarHidden = NO;
libraryPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:libraryPicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
chosenImage = [info objectForKey:UIImagePickerControllerOriginalImage];
controller.photo = chosenImage;
[libraryPicker dismissViewControllerAnimated:NO completion:nil];
[self presentViewController:controller animated:YES completion:nil];
}
PhotoViewController:
@property (strong,readwrite) IBOutlet UIImageView *photoView;
@property (strong,readwrite) UIImage *photo;
+ (PhotoViewController *)sharedInstance;
static PhotoViewController *_self;
+ (PhotoViewController*)sharedInstance
{
if (_self == nil) {
_self = [[super alloc] init];
}
return _self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
photoView.image = photo;
}
So when I choose a pic, the app shows a black screen without errors in debug area. What's wrong?
P.S.: I'm using Storyboard and ARC
Upvotes: 0
Views: 1008
Reputation: 12842
Couple things to note:
PhotoViewController
is designed in the storyboard, the way that you are instantiating it in sharedInstance
is not correct. It is only creating a plain PhotoViewController
with none of the Interface Builder objects created.
SecondViewController
and PhotoViewController
with a manual, modal segue in the storyboard. Then in, SecondViewController
in didFinishPickingMediaWithInfo
, set the photo on PhotoViewController and manually trigger the segue.viewDidLoad
of PhotoViewController
will only be called the first
time it's presented, so it's not the right place to set the photo for
subsequent launches. Consider viewWillAppear
photoView
is connected between the property and the Interface
Builder element in the storyboardUpvotes: 1