objective-c - 未收到通知回复

标签 objective-c cocoa osx-mavericks nsusernotification nsusernotificationcenter

我正在尝试从通知中获取回复并将字符串传递给标签。 但是我回复后标签(nResponse)没有改变,为什么标签没有更新? “activationType”方法有问题吗?

AppDelegate.h

@property (weak) IBOutlet NSTextField *nTitle;
@property (weak) IBOutlet NSTextField *nMessage;
-(IBAction)showNotification:(id)sender;
@property (weak) IBOutlet NSTextField *nResponse;
@property (assign) IBOutlet NSWindow *window;
@property BOOL hasReplyButton NS_AVAILABLE(10_9, NA);
@property (copy) NSString *responsePlaceholder NS_AVAILABLE(10_9, NA);
@property (readonly) NSAttributedString *response NS_AVAILABLE(10_9, NA);
@property (copy) NSImage *contentImage NS_AVAILABLE(10_9, NA);
@end

AppDelegate.m

@synthesize nTitle;
@synthesize nMessage;
@synthesize nResponse;
@synthesize window;

- (IBAction)showNotification:(id)sender{
    NSUserNotification *notification = [[NSUserNotification alloc] init];
    notification.title = [nTitle stringValue];
    notification.informativeText = [nMessage stringValue];
    notification.soundName = NSUserNotificationDefaultSoundName;
    notification.responsePlaceholder = @"Reply";
    notification.hasReplyButton = true;

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification;
{
    if (notification.activationType == NSUserNotificationActivationTypeReplied){
        NSString* userResponse = notification.response.string;
        [nResponse setStringValue:userResponse]; //set label to response
        NSLog(@"%@", userResponse);
    }
    return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}

@end

最佳答案

NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = self;

您还应该确保您的通知类实现 NSUserNotificationCenterDelegate 协议(protocol)。

关于objective-c - 未收到通知回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967794/

相关文章:

iphone - 如何用多个其他字符串 [NSString] 替换多个字符串的出现

ios - 如何从自定义 Timer 对象更新 View

objective-c - NSTextView 选择突出显示所有字符甚至段落缩进

java - 与 MacBook Pro 上的 64 位 Eclipse 不兼容的 JVM 错误消息

haskell - cabal install cabal-install 无法解决依赖关系

iphone - 删除给定 NSRange 的子字符串

objective-c - 有什么方法可以加粗 NSString 的一部分?

macos - NSWindow 显示的建议

Objective-C 系列 - Mac OS X

python - 升级到 Mavericks 后 Bpython 无法工作