[英]Black screen after presentViewController
I got a UIViewController called SecondViewController which loads the photo library using UIImagePickerController. 我有一个名为SecondViewController的UIViewController,它使用UIImagePickerController加载照片库。 When I select a photo, it should present another view controller called PhotoViewController. 当我选择一张照片时,它应该显示另一个名为PhotoViewController的视图控制器。 To pass the image from SecondViewController to PhotoViewController I use a singleton. 要将图像从SecondViewController传递到PhotoViewController,我使用单例。 Here is the code: 这是代码:
SecondViewController: 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: 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? 怎么了?
PS: I'm using Storyboard and ARC PS:我正在使用Storyboard和ARC
Couple things to note: 需要注意的几件事:
PhotoViewController
is designed in the storyboard, the way that you are instantiating it in sharedInstance
is not correct. 如果PhotoViewController
是在情节sharedInstance
设计的,则在sharedInstance
中实例化它的sharedInstance
是不正确的。 It is only creating a plain PhotoViewController
with none of the Interface Builder objects created. 它只是创建一个普通的PhotoViewController
,而没有创建任何Interface Builder对象。
SecondViewController
and PhotoViewController
with a manual, modal segue in the storyboard. 在这里,一种更好的方法是将第二SecondViewController
和PhotoViewController
的场景与情节SecondViewController
的手动模态segue相连接。 Then in, SecondViewController
in didFinishPickingMediaWithInfo
, set the photo on PhotoViewController and manually trigger the segue. 然后,在didFinishPickingMediaWithInfo
中的SecondViewController
中,在PhotoViewController上设置照片并手动触发序列。 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. PhotoViewController
viewDidLoad
仅在首次显示时被调用,因此它不是为后续启动设置照片的合适位置。 Consider viewWillAppear
考虑viewWillAppear
photoView
is connected between the property and the Interface Builder element in the storyboard 确认在属性和情节photoView
中的Interface Builder元素之间连接了photoView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.