注册NSNotification会导致我的应用程序崩溃?

时间:2010-10-29 07:16:20

标签: iphone iphone-sdk-3.0 ios4 nsnotifications

我正在使用 MPMovieplayerViewController 播放电影,我想在电影停止时注册通知...我正在使用以下代码来使用 NSNotification 但我的应用程序当电影停止时崩溃......我早先用同样的方式使用 NSNotification ,当它执行得很好的时候......关于我做错了什么的想法?

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playbackFinishedCallback:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:movie];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:nil];
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{

NSLog(@"moviePlayBackDidFinish");
    MPMoviePlayerViewController *movie = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:movie];


[self performSelector:@selector(stopRecording) withObject:nil afterDelay:1.0];

}

-(void)playbackFinishedCallback:(NSNotification *)notification{

MPMoviePlayerViewController *movie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:movie];

NSLog(@"playbackFinishedCallback:");


[self performSelector:@selector(stopRecording) withObject:nil afterDelay:1.0];


}

在我的AppDelegate类中,我已经注册了这样的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


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



// Override point for customization after application launch.

// Add the navigation controller's view to the window and display.
[window addSubview:navigationController.view];
[window makeKeyAndVisible];

return YES;
}

- (void)dealloc {

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:nil];

[navigationController release];
[window release];
[super dealloc];

}

1 个答案:

答案 0 :(得分:1)

也许就是这样:

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

在上面的行中,您传递一个nil对象,并在方法中尝试获取它:

MPMoviePlayerViewController *movie = [notification object];
相关问题