如何在运行时调用IPad中的第二个XIB?

时间:2012-06-01 11:01:14

标签: ios ipad mpmovieplayercontroller xib

在我的应用程序中,我使用MPMoviePlayerController在我的第一个类XIB中运行视频。我的视频durtion大约是20秒。我希望当我的视频结束时它会自动调用第二类XIB。这是我的代码。

     -(void)viewWillAppear:(BOOL)animated
       {
         NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"3idiots.mov" ofType:nil];
         NSURL *url = [NSURL fileURLWithPath:urlStr];
         videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
         [self.view addSubview:videoPlayer.view];
         videoPlayer.view.frame = CGRectMake(0, 0,768, 1000);  
         [videoPlayer play];
         [self performSelector:@selector(gotonextview)];

         }
      -(void)gotonextview
        {
         secondview *sec=[[secondview alloc] initWithNibName:@"secondview" bundle:nil];
         [self presentModalViewController:sec animated:YES];
         [sec release];

        }

这段代码给我没有错误,但它不会在视频完成后调用二等。任何人都可以指导我。 Thanx提前

3 个答案:

答案 0 :(得分:3)

这些都在文档中解释...也是iOS版本之间的各种行为。

请勿从viewWillAppear调用gotonextview。而是在viewDidLoad中将视图控制器注册为MPMoviePlayerPlaybackDidFinishNotification和MPMoviePlayerDidExitFullscreenNotification的观察者,并将gotonextview:(NSNotification *)通知作为选择器。

另外,我建议你从viewDidAppear而不是viewWillAppear启动电影播放器​​。

编辑:改编原始海报代码(未经测试)......

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotonextview:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotonextview:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
}

-(void)viewDidAppear:(BOOL)animated
{
    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"3idiots.mov" ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.view addSubview:videoPlayer.view];
    videoPlayer.view.frame = CGRectMake(0, 0,768, 1000);  
    [videoPlayer play];
}

-(void)gotonextview:(NSNotification *)notification
{
    NSDictionary *notifDict = notification.userInfo; // Please refer Apple's docs for using information provided in this dictionary

    secondview *sec=[[secondview alloc] initWithNibName:@"secondview" bundle:nil];
    [self presentModalViewController:sec animated:YES];
    [sec release];

}

答案 1 :(得分:0)

一个选项是:

使用带有两个选项卡的tabBarController。将视频放在一个标签中,然后将“第二个视图”放在第二个标签中。然后使用

self.tabBarController.selectedIndex=2;

答案 2 :(得分:0)

更好的方法是使用Timer或向视频播放器注册事件监听器。

可以在http://mobiledevelopertips.com/cocoa/basics-of-notifications.html

下找到示例