考虑到现实生活中的情况,假设我已将一些工作分配给 3 个人(例如 A、B、C),我希望每个人都完成所有分配的任务,而不是等待他们每个人完成一项任务,他/她会明确通知我。这样我就可以根据他/她的任务做出进一步的决定。
我想在代码中实现这种情况,而不使用单独的线程和委托(delegate),我的意思是使用 NSNotification。
我如何通过编程来完成这些事情,你可以使用代码(iPhone SDK-Objective C)解决上述情况吗?
最佳答案
[[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。希望您能理解。
关于iphone - 如何在以下场景(iPhone SDK)中使用NSNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5832027/