macos - 如何更新交付的 NSUserNotification 中的副标题?

标签 macos cocoa nsusernotification nsusernotificationcenter

我正在尝试更新已发送通知(警报)的副标题中的字符串,我使用 NSTimer 执行此操作,如下所示:

[NSTimer scheduledTimerWithTimeInterval:5.0
                                 target:self
                               selector:@selector(updateSubtitle)
                               userInfo:nil
                                repeats:YES];


- (void)updateSubtitle
{
    [[NSUserNotificationCenter defaultUserNotificationCenter].deliveredNotifications 
        enumerateObjectsUsingBlock:^(NSUserNotification *notification, NSUInteger idx, BOOL *stop) {
            notification.subtitle = @"new subtitle";
    }];
}

代码每 5 秒正确执行一次。但通知警报中显示的副标题不会改变。

是否有某种方法可以强制“重绘”,例如 setNeedsDisplay 或类似的东西?

谢谢。

最佳答案

我知道这篇文章有点旧,但我最近试图弄清楚同样的事情,但最终需要删除现有的通知并添加一个具有相同标题的新通知。

当我创建通知时,我在通知的标识符字段中使用 NSUUID,然后使用类似(在 Swift 中)的方式查找现有的通知:

var notif = NSUserNotification()
notif.identifier = <Id To Search For>
NSUserNotificationCenter.defaultUserNotificationCenter().removeScheduledNotification(notif)
NSUserNotificationCenter.defaultUserNotificationCenter().removeDeliveredNotification(notif)

关于macos - 如何更新交付的 NSUserNotification 中的副标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24514996/

相关文章:

macos - 如何从mac终端打开一个窗口

objective-c - 使用未声明的标识符 ' IKImageBrowserNSURLRepresentationType'

multithreading - Cocoa:延迟一个方法调用,直到处理后续调用

objective-c - 检测其他按钮点击(NSUserNotification)

ios - Swift:将本地通知转换为用户通知 IOS 10

macos - 应用程序可以在通知中心同时使用警报和横幅吗?

objective-c - AVCaptureVideoPreviewLayer 不显示

ios - Apple 设备上发生错误定向请求

iphone - XCode 4 和 XCode 3.2.6 并行

cocoa - 使用解码数组创建 CGImage 时,输出的所有像素都会有少量偏移