2

考虑到现实生活中的情况,假设我给 3 个人分配了一些工作(比如 A 人、B 人、C 人),而不是等待他们分别完成一项任务,我希望当每个人完成所有分配的任务时,他/她会清楚地通知我。这样我就可以根据他/她的任务做出进一步的决定。

我想在代码中实现这种情况,而不使用单独的线程和委托,我的意思是使用 NSNotification。

我怎么能用编程来做这些事情,你能用代码(iPhone SDK-Objective C)解决上述情况吗?

4

2 回答 2

1
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer.moviePlayer];


- (void)myMovieFinishedCallback:(NSNotification*)aNotification
{    
    // Release the movie instance created in playMovieAtURL

    MPMoviePlayerController *theMovie = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:theMovie];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIDeviceOrientationDidChangeNotification
                                                  object:theMovie]; 

    [theMovie stop];
    [theMovie.view removeFromSuperview];

} 

像这样你可以使用 NSNotification。希望你能理解。

于 2011-04-29T12:34:34.200 回答
1

只要您不使用单独的线程(或某种模拟的异步性),这 3 个人将需要互相等待并且使用通知并没有真正意义。

于 2011-04-29T12:38:39.073 回答