iphone - 如何在以下场景(iPhone SDK)中使用NSNotification?

标签 iphone nsnotification

考虑到现实生活中的情况,假设我已将一些工作分配给 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/

相关文章:

iphone - 更改图层/ View 属性时禁用动画?

ios - 如何根据 UIlabel 文本大小动态调整单元格大小

iphone - .Xib使用Xcode 4.6.1运行项目时创建错误

iphone - 应用程序中多个 UIWindow 的最佳实践

ios - 收到 NSNotification 后执行选择器会调用我的 View Controller 的 deinit

ios - 当应用程序在后台时,确定用户是否没有点击通知

ios - NSNotification postNotification 和 addObserver

iphone - self.myIvar 和 myIvar 之间有什么不同?

cocoa - 无法通过通知中心生成 "user switch notifications work"

objective-c - 将 NSNotification 从 classA 发送到 classB