0

通知から返信を得て、文字列をラベルに渡そうとしています。しかし、返信してもラベル (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
4

1 に答える 1

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

また、Notice クラスがNSUserNotificationCenterDelegateプロトコルを実装していることを確認する必要があります。

于 2013-11-22T12:25:13.650 に答える