gpt4 book ai didi

ios - presentViewController 后黑屏

转载 作者:行者123 更新时间:2023-11-29 03:20:15 26 4
gpt4 key购买 nike

我有一个名为 SecondViewController 的 UIViewController,它使用 UIImagePickerController 加载照片库。当我选择一张照片时,它应该显示另一个名为 PhotoViewController 的 View Controller 。为了将图像从 SecondViewController 传递到 PhotoViewController,我使用了单例。这是代码:

第二 View Controller :

@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];
}

照片 View Controller :

@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

最佳答案

注意事项:

  • 如果 PhotoViewController 是在 Storyboard中设计的,则您在 sharedInstance 中实例化它的方式不正确。它只是创建一个普通的 PhotoViewController,没有创建任何 Interface Builder 对象。
    • 此处更好的方法是将 SecondViewControllerPhotoViewController 的场景与 Storyboard 中的手动模式转场连接起来。然后在 didFinishPickingMediaWithInfo 中的 SecondViewController 中,在 PhotoViewController 上设置照片并手动触发 segue。
  • PhotoViewController
  • viewDidLoad 只会被第一个调用它出现的时间,所以它不是设置照片的正确位置随后的发射。考虑 viewWillAppear
  • 确认 photoView 连接在属性和接口(interface)之间 Storyboard中的构建器元素

关于ios - presentViewController 后黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21190840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com