0

如何从带有回复字段的通知中获取输入。我在文档中没有找到任何东西

这是我当前的代码,我需要什么才能得到用户的响应?

#import "AppDelegate.h"

@implementation AppDelegate
@synthesize nTitle;
@synthesize nMessage;

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

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{
    return YES;
}

@end

我在哪里可以找到通知中心的更多最新信息和文档?

4

2 回答 2

7

您可以像往常一样在标题中找到最新的文档。首先,确保您使用的是 OSX SDK 10.9。几乎没有带有描述的新字段。

NSUserNotification.h:

// Set to YES if the notification has a reply button. The default value is NO.
// If both this and hasActionButton are YES, the reply button will be shown.
@property BOOL hasReplyButton NS_AVAILABLE(10_9, NA);

// Optional placeholder for inline reply field.
@property (copy) NSString *responsePlaceholder NS_AVAILABLE(10_9, NA);

// When a notification has been responded to, the NSUserNotificationCenter delegate
// didActivateNotification: will be called with the notification with the activationType
// set to NSUserNotificationActivationTypeReplied and the response set on the response property
@property (readonly) NSAttributedString *response NS_AVAILABLE(10_9, NA);

我们开始做吧:

- (IBAction)showNotification:(id)sender{
NSUserNotification *notification = [[NSUserNotification alloc] init];
...
notification.responsePlaceholder = @"Reply";
notification.hasReplyButton = true;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
    if (notification.activationType == NSUserNotificationActivationTypeReplied){
        NSString* userResponse = notification.response.string;
    }
}

请注意,在鼠标移到通知窗口之外之前,回复按钮是隐藏的,并且在单击按钮后将显示回复字段。

于 2013-11-11T20:58:20.963 回答
1

如果您搜索“NSUserNotificationCenter”、“NSUserNotification”和“NSUserNotificationCenterDelegate 协议参考”,您可以在 Xcode 5 的内联文档中找到您需要的所有文档。

用户通知中没有回复字段,但您可以添加一个操作按钮并测试用户是否单击了此按钮或默认按钮以关闭通知。但是,如果在通知中心首选项中,用户选择接收横幅而不是警报,您将永远不会收到响应通知,因为横幅中没有按钮。

于 2013-11-11T10:01:38.223 回答