【问题标题】:Error when calling the controller iOS调用控制器 iOS 时出错
【发布时间】:2015-04-08 10:10:54
【问题描述】:

请帮助理解困难的情况。

我实现了Pop Up Window,方式已经描述here

然后,在这个窗口中,我决定从库中实现一个示例照片,但是当您打开一个新控制器时可能会收到以下警告:

Presenting view controllers on detached view controllers is discouraged <PopUpViewController: 0x7fbb405f4e90>.

示例照片实现如下:

picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = NO;
        [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController:picker animated:YES completion:nil];

【问题讨论】:

    标签: ios view uiimagepickercontroller transition


    【解决方案1】:

    试试这个:

    [self.view.window.rootViewController presentViewController:picker animated:YES completion:nil];
    

    【讨论】:

    • 警告:尝试在视图不在窗口层次结构中的 上呈现
    • 尝试:[self.parentViewController presentViewController:picker animated:YES completion:nil];注意 picker 不是 UIViewController
    • 我从我的应用程序中复制了这些示例代码,它可以工作,你和我之间的唯一区别是我提供的是 UIViewController 而不是 UIImagePickerController
    • 在 iPhone 5s 模拟器上运行并点击照片按钮
    • 按照片按钮,然后选择库
    【解决方案2】:

    我认为您在 viewDidAppear 方法中展示了您的选择器。

    如果是这样,那就是警告的原因......

    请编写另一个方法来显示我们的选择器,然后延迟执行。

    [self performSelector:@selector(yourMethod)
               withObject:nil afterDelay:0.0];
    

    这将使您的presentingController 在呈现另一个控制器之前完全加载。

    我希望这会有所帮助..

    编辑

    问题是,您在操作表被关闭之前就展示了选择器

    编辑 2

    也可以代替actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex,使用此方法获取点击索引 -

    - actionSheet:didDismissWithButtonIndex:
    

    【讨论】:

    • 不,我不使用 viewDidAppear 方法。我使用:actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    • @AlexeySlivkin 你确定会出现关于UIImagePickerController 的警告吗?我在准确呈现 UIActionSheet 时遇到了类似的警告
    • 警告恰好出现在弹出窗口中。在另一个控制器上效果很好
    • 您是否尝试编写其他方法,然后按照我的回答调用它
    • 你在我的回答中检查了 EDIT 2 吗?
    【解决方案3】:

    也许您需要使用addChildController 将弹出窗口作为子控制器添加到您从中显示它的控制器中,这样它就不会从视图层次结构中分离出来?

    您也可以尝试:另一种选择:

    [self.parentViewController presentViewController:picker animated:YES completion:nil];
    

    我在另一个线程上注意到了这一点作为解决方案。 Warning :-Presenting view controllers on detached view controllers is discouraged

    在你的情况下,parentViewController 很可能是 nil。

    查看代码后编辑:

    查看代码后,问题似乎如下:

    1) 弹出机制是使用 UIViewController 的实例来创建布局,但您实际上根本没有使用视图控制器。弹出机制 (showInView:withImage:withMessage:animated:) 是从控制器中提取视图并将其添加到请求弹出的控制器的视图层次结构中。因此,使用此弹出控制器实例中调用[self presentViewController...] 的代码会从实际上未使用的控制器中呈现模态控制器。我相信这就是错误的来源。您需要从原始控制器执行所有警报和模式演示,或者传入控制器以使用。

    2) 原文章中提到因为只用到了控制器的容器视图,所以在创建的时候需要保持对它的强引用。所以你应该有:

    self.popViewController = [[PopUpViewController alloc] initWithNibName:@"PopUpViewController" bundle:nil];
    

    解决方案:

    我将扩展showInView... 方法以传入拥有正在拼接弹出窗口的视图的控制器。然后使用此控制器呈现任何模态控制器。所以改变:

    - (void)showInView:(UIView *)aView withImage:(UIImage *)image withMessage:(NSString *)message animated:(BOOL)animated
    

    - (void)showInController:(UIViewController *)aController inView:(UIView *)aView withImage:(UIImage *)image withMessage:(NSString *)message animated:(BOOL)animated
    

    将控制器引用存储在PopUpViewController 类中的属性中,然后使用该属性调用presentViewController...

    【讨论】:

    • 我尝试了那里描述的所有解决方案 - 没有帮助
    • 我注意到在您发布的代码中,您没有为弹出控制器维护一个强大的参考。我认为您想为弹出窗口创建一个强大的属性并拥有self.popViewController = [[PopUpViewController alloc] initWithNibName:@"PopUpViewController" bundle:nil]; 这在您使用的代码的一些博客 cmets 中有所提及。
    • 查看您发布的代码后更新。
    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 2017-12-26
    相关资源
    最近更新 更多