我正在构建一个非常简单的 iOS iPad 应用程序,它由一个视图和 4 个按钮组成。所以基本上你在故事板中有:
->ViewController
->View (this is added just for alignment and position sake, nothing else)
->View
->Button1
->Button2
->Button3
->Button4
当您按下一个按钮时,电影将以全屏模式播放,所有 4 个按钮都相同。一旦电影完成,要么是因为它完成了,要么是用户按下了“完成”,
[moviePlayer.view removeFromSuperview]
用于删除电影,一切都回到应用程序的初始状态,有4个按钮。
这是按下按钮时播放电影的代码
- (void) playMovie:(NSString *)fileName
ofType:(NSString *)fileType
{
NSString *filePath=[[NSBundle mainBundle] pathForResource:fileName ofType:fileType];
NSURL *fileUrl=[NSURL fileURLWithPath:filePath];
self.moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:fileUrl];
[self.view addSubview:self.moviePlayer.view];
self.moviePlayer.fullscreen = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerWillExitFullscreenNotification
object:self.moviePlayer];
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
}
这是我用来停止和删除电影的代码:
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerWillExitFullscreenNotification
object:player];
if ([player respondsToSelector:@selector(setFullscreen:animated:)])
{
[player stop];
[player setFullscreen:NO animated:YES];
[player.view removeFromSuperview];
}
}
我遇到的问题是,在[player.view removeFromSuperview];
执行之后,我返回到初始视图,没有背景图像(它变成黑色)并且任何按钮都没有响应。
如果我删除包含按钮的视图,并将按钮添加到主视图,它会按预期工作。
抱歉,如果这不是太清楚,我已经浏览了书籍和许多网站,但似乎无法理解这一点。
干杯!