1

我一直在按照自己的节奏学习 Swift,在学习了如何制作 NSUserNotifications 之后,我已经深入研究了通知按钮,并让它们做事。

我知道如何使用 AppDelegate.swift 中的以下内容使单个通知的操作按钮执行操作

func userNotificationCenter(center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification) 
{
  print("You clicked the notification button!")
}

问题是,我如何让第二个做同样的事情?在我问这个问题之前,我已经在 SO、reddit 和众多 Swift 教程网站上搜索了一个易于理解的答案,因为在 C# 中委托和覆盖对我来说没有问题,我得到了概念,我完全不知道 Swift 如何做到这一点。

思考过程

因为didActivateNotification notification: NSUserNotification" inAppDelegate.swift包含标识符 "notification",如果我再次声明,它也是我的 ViewController 类中我的第一个 NSUserNotification 对象的标识符userNotificationCenter(),并将 "didActivateNotification 通知:NSUserNotification" 更改为 "didActivateNotification 通知:NSUserNotification",用于我的第二个 NSUserNotification具有标识符“notif”的对象,它应该为我的第二个通知的操作按钮执行相关操作,该按钮正在向控制台打印一条略有不同的消息,对吧?

不用说,它没有用。

我在 UI 方面做了什么

我有两个按钮,每个按钮都会生成一个带有不同文本的通知,还有一个动作按钮(据说)会做不同的事情。

包起来

那么,我需要做什么才能让两个(或更多)通知的操作按钮执行它们各自的操作?

非常感谢您的任何帮助,并提前表示感谢。

4

0 回答 0