[英]Presenting a View Controller after a UIPopoverController is dismissed
I am having some difficulty presenting a full screen View Controller (specifically, an MPMoviePlayerViewController
) immediately after dismissing a UIPopoverController
. 我在解散
UIPopoverController
之后立即呈现全屏视图控制器(特别是MPMoviePlayerViewController
)时遇到了一些困难。 Essentially, I have a race condition and I'm not sure if there's an accepted best practice to correct it. 本质上,我有比赛条件,而且不确定是否有公认的最佳做法来纠正它。 Here is the code I started with:
这是我开始的代码:
[[self searchPopoverController] dismissPopoverAnimated:YES];
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:[video videoURL]];
[[player moviePlayer] setFullscreen:YES animated:YES];
[self presentMoviePlayerViewControllerAnimated:player];
The problem is that the popover doesn't actually dismiss before the next lines of code are executed, resulting (as expected) in an "Attempt to present [a view controller] while a presentation is in progress" warning. 问题在于,在执行下一行代码之前,弹出窗口实际上并没有消除,从而(按预期)导致“在演示过程中尝试呈现[视图控制器]”警告。 Somewhat humorously, the video does start playing (you can hear the audio), but the view controller is not presented so you cannot see the video.
有点幽默,视频确实开始播放(您可以听到音频),但是未显示视图控制器,因此您无法观看视频。
I've tried fixing this in several ways. 我已尝试通过几种方式解决此问题。 The only reliable solution I have found is ugly and not a guaranteed fix:
我发现的唯一可靠的解决方案是丑陋的,而不是可以保证的解决方案:
[[self searchPopoverController] dismissPopoverAnimated:YES];
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
dispatch_after(start, dispatch_get_main_queue(), ^(void){
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:[video videoURL]];
[[player moviePlayer] setFullscreen:YES animated:YES];
[self presentMoviePlayerViewControllerAnimated:player];
});
Since UIPopoverController
does not inherit from UIViewController
, I do not believe there is any method for dismissing the popover that includes a completion-handling block. 由于
UIPopoverController
不能从UIViewController
继承,因此我不认为有任何方法可以消除包含完成处理块的弹出窗口。 I've tried using NSInvocationOperation
and NSOperationQueue
, with no success, for example: 我尝试使用
NSInvocationOperation
和NSOperationQueue
,但是没有成功,例如:
NSInvocationOperation *invokedOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(playVideo:) object:video];
[[NSOperationQueue mainQueue] addOperation:invokedOperation];
There are lots of existing questions that touch on this issue, but most seem to revolve around dismissing a UIViewController
or subclass, for which dismissViewControllerAnimated:completion:
is an obvious solution. 有很多有关此问题的现有问题,但大多数问题似乎都围绕着解散
UIViewController
或子类,而dismissViewControllerAnimated:completion:
是一个显而易见的解决方案。
You should use the popover delegate method popoverControllerDidDismissPopover
您应该使用
popoverControllerDidDismissPopover
委托方法popoverControllerDidDismissPopover
This will notify you once the pop over has fully been dismissed. 一旦弹出窗口被完全消除,这将通知您。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverControllerDelegate_protocol/index.html#//apple_ref/occ/intfm/UIPopoverControllerDelegate/popoverControllerDidDismissPopover :
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverControllerDelegate_protocol/index.html#//apple_ref/occ/intfm/UIPopoverControllerDelegate/popoverControllerDidDismissPopover :
为什么不实现UIPopoverControllerDelegate协议并将您的控制器添加为委托,那么您可以将第二个viewController从
-(void)popoverControllerDidDismissPopover:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.