Error:whose view is not in the window hierarchy - iOS

今天在调用播放视频控件的时候,当 MPMoviePlayerViewController 成功读取 url Link 加载执行 presentViewController 操作时遇到了异常;

异常导致视频 push 进 view 后屏幕处于黑屏状态,只发音却未渲染出图像内容;

通过控制台的 Error 提示捕获到 whose view is not in the window hierarchy! 该异常提示;

通过 debug 后发现是当前要渲染的页面未被初始化就调用了 VC 内的方法,导致视图未能成功加载出来.


解决办法:

在执行完 code 逻辑后,即将调用 presentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#> 方法前添加如下代码即可,代码如下:

一.代码创建视图方法

while (vc.presentedViewController) {// 解决 whose view is not in the window hierarchy!

    vc = vc.presentedViewController;

}


二.Storyboard 创建视图方法

UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

LoginViewController* loginViewController = [mainstoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];

[self.window makeKeyAndVisible];

//[LoginViewController presentViewController:yourController animated:YES completion:nil];

//or

[LoginViewController myMethod];

其中,上述 code 中的 vc 为 UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController; 即你将要 presentViewController 的 VC.


示例 Code 如下:

UIViewController *vc = kKeyWindow.rootViewController;

NSString *video = @"";

NSString *src = [NSString stringWithFormat:@"%@", dicCommand[@"args"][@"src"]];

if (!kStringIsEmpty(src)) {

    if ([src rangeOfString:@".mp4"].location != NSNotFound) {// 当前字符串中是否包含 .mp4

    video = src;

    }

    else {

        video = [NSString stringWithFormat:@"%@.mp4", src];

    }

    NSLog(@"Current Video Link --- \n%@", video);

    MPMoviePlayerViewController *mpc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:video]];

    while (vc.presentedViewController) {// 解决 whose view is not in the window hierarchy!

        vc = vc.presentedViewController;

    }

        [vc presentViewController:mpc animated:YES completion:nil];

    }

else {

    [MBProgressHUD showError:@"暂无视频资源" toView:vc.view];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值