presentViewController之后的黑屏

时间:2014-01-17 16:26:21

标签: ios objective-c cocoa-touch uiviewcontroller

我有一个名为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

1 个答案:

答案 0 :(得分:1)

要注意事项:

  • 如果在故事板中设计了PhotoViewController,那么您在sharedInstance中实例化它的方式是不正确的。它只创建一个没有创建Interface Builder对象的普通PhotoViewController
    • 这里更好的方法是将SecondViewControllerPhotoViewController的场景与故事板中的手动模态segue联系起来。然后在SecondViewController didFinishPickingMediaWithInfo中,在PhotoViewController上设置照片并手动触发segue。
  • viewDidLoad PhotoViewController只会被称为第一个 它呈现的时间,因此它不是设置照片的正确位置 随后的发布。考虑viewWillAppear
  • 确认属性和接口之间已连接photoView 故事板中的Builder元素