gpt4 book ai didi

objective-c - 未收到通知回复

转载 作者:行者123 更新时间:2023-12-03 17:39:59 24 4
gpt4 key购买 nike

我正在尝试从通知中获取回复并将字符串传递给标签。但是我回复后标签(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com