我有一个名为SecondViewController的UIViewController,它使用UIImagePickerController加载照片库。当我选择一张照片时,它应该呈现另一个名为PhotoViewController的视图控制器。要将图像从SecondViewController传递到PhotoViewController,我使用单例。这是代码:
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;
}
因此,当我选择一张照片时,该应用程序在调试区域显示黑屏而没有错误。怎么了?
P.S。:我正在使用Storyboard和ARC
答案 0 :(得分:1)
要注意事项:
PhotoViewController
,那么您在sharedInstance
中实例化它的方式是不正确的。它只创建一个没有创建Interface Builder对象的普通PhotoViewController
。
SecondViewController
和PhotoViewController
的场景与故事板中的手动模态segue联系起来。然后在SecondViewController
didFinishPickingMediaWithInfo
中,在PhotoViewController上设置照片并手动触发segue。viewDidLoad
PhotoViewController
只会被称为第一个
它呈现的时间,因此它不是设置照片的正确位置
随后的发布。考虑viewWillAppear
photoView
故事板中的Builder元素