我想显示用户通知,而应用程序是最正面的。我在下面找到了代码,但我不知道如何使用委托:它似乎只是返回一个布尔值。
class MyNotificationDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self
}
func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
return true
} }我试过一些句子,比如:
var delegate : MyNotificationDelegate = MyNotificationDelegate()
var notification:NSUserNotification = NSUserNotification()
var notificationcenter:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
delegate.userNotificationCenter(notificationcenter, shouldPresentNotification: notification)但它不会展示横幅。我知道,对于NSUserNotificationCenter,deliverNotification:方法是显示横幅的方法。但我不确定NSUserNotificationCenterDelegate协议。
如何总是显示通知横幅?
发布于 2015-10-21 06:05:30
您的通知中心委托和委托方法应该在AppDelegate中实现,然后才能工作。如果您在任何其他类中实现,它将不会以横幅形式出现,尽管它在通知面板中无声地出现。
我试着做了以下工作:
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
let notification: MyNotificationDelegate = MyNotificationDelegate()
NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self;
notification.setNotification("Hi", message: "How are you?")
}
func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
return true
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
class MyNotificationDelegate: NSObject {
func setNotification(title: String, message: String)
{
let notification: NSUserNotification = NSUserNotification()
notification.title = title
notification.informativeText = message
NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)
}
}https://stackoverflow.com/questions/31869040
复制相似问题