2012-06-01 12 views
0

在我的應用程序中,我使用MPMoviePlayerController.my在第一類XIB中運行視頻,視頻durtion大約是20秒。我希望當我的視頻結束時自動調用第二類XIB.here是我的代碼。如何在運行時調用IPad中的第二個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

這是所有在文檔中解釋的...還有iOS版本之間的各種行爲。

不要從viewWillAppear調用gotonextview。相反,將viewDidLoad作爲觀察者註冊爲MPMoviePlayerPlaybackDidFinishNotification和MPMoviePlayerDidExitFullscreenNotification,並將gotonextview:(NSNotification *)通知作爲選擇器註冊到viewDidLoad中。

此外,我建議你從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

「不要叫viewWillAppear中而不是gotonextview註冊您的視圖控制器作爲MPMoviePlayerPlaybackDidFinishNotification和MPMoviePlayerDidExitFullscreenNotification在viewDidLoad中與gotonextview觀察員:(NSNotification *)通知的選擇。」確保您在viewDidLoad NOT viewDidAppear中註冊了這些通知... viewDidAppear用於開始播放。 –

+0

thanx的回覆所以for.i我無法理解你的方法可以請您使用我的代碼編輯您的答案..thanx – jamil

+0

我已適應您的代碼並編輯我的原始帖子。它沒有經過測試,但至少應該把你放在正確的軌道上。請閱讀蘋果文檔以獲取更多關於使用的MPMoviePlayerController和NSNotification –

0

一個選項是:

使用帶有兩個選項卡的tabBarController。將您的視頻放在一個標籤中,並將第二個視圖放在第二個標籤中。然後使用

self.tabBarController.selectedIndex=2; 
+1

中查到,但是這並沒有真正回答這個問題做的。 –

相關問題