使用回复字段创建通知

时间:2013-11-11 01:06:06

标签: cocoa input osx-mavericks nsusernotificationcenter

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

以下是我当前的代码,我需要从用户那里得到什么回复?

#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

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

2 个答案:

答案 0 :(得分: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;
    }
}

请注意,回复按钮是隐藏的,直到鼠标位于通知窗口之外,并且点击按钮后将显示回复字段。

答案 1 :(得分:1)

如果搜索“NSUserNotificationCenter”,“NSUserNotification”和“NSUserNotificationCenterDelegate Protocol Reference”,您可以在Xcode 5的内联文档中找到所需的所有文档。

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