Frage

I am trying to get a reply from a notification and pass the string to a label. But the label (nResponse) does not change after I give the reply, why isn't the label updating? Is there a problem with the "activationType" method?

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
War es hilfreich?

Lösung

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

You should also make sure your Notice class implements the NSUserNotificationCenterDelegate protocol.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top