ios - NSNotification 延迟

标签 ios swift ios9 nsnotificationcenter

如何延迟发布NSNotification

我想到的一个解决方案:- 在 performSelectorAfterDelay 方法中发布通知。有没有更好的解决方案。

NSNotificationQueue可以帮助我实现这一目标吗?

最佳答案

利用 GCD 的 dispatch_after()方法。在 Objective-C 中,这看起来像:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [[NSNotificationCenter defaultCenter] postNotification:someNotification];
});

更新:Swift 3

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(5)) { 
    NotificationCenter.default.post(someNotification)
}

关于ios - NSNotification 延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35474589/

相关文章:

ios - 从另一个 Storyboard呈现 View Controller

iOS应用程序数据库和云存储

ios - SQLCipher编译错误

ios - 将 UTC 时间转换为当前时区时间时转换为错误的时间

ios - iOS 9 中的 UIAlertView 问题

ios - 从 NSDate 获取正确日期的问题

iOS:如何将 css 文件注入(inject)到从 Swift 中的外部 url 加载的 webview 链接

ios - 如果我使用 unwindSegue 从屏幕 A 转到 B 再回到 B,如何返回到我的主显示器 (A)

ios - 打开功能后,iOS 9/10 中未收到 Apple 推送通知

ios - Swift:在 SplitViewController 之前有一个登录屏幕