首页 > 其他 > 详细

【转载】User notification 的实现方法

时间:2014-12-22 17:44:52      阅读:255      评论:0      收藏:0      [点我收藏+]

原帖请看:http://cocoathings.blogspot.com/2013/01/introduction-to-user-notifications-in.html

 

想要实现如图这样的notification popup

技术分享

技术分享

弹出notification的代码如下

NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = @"Hello, World!";
notification.informativeText = [NSString stringWithFormat:@"details details details"];
notification.soundName = NSUserNotificationDefaultSoundName;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];

但是这个popup并不是默认显示的,想要让他show出来还必须在程序里实现下面的方法:

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
     shouldPresentNotification:(NSUserNotification *)notification
{
    return YES;
}

然后把NSUserNotificationCenter.的delegate设置为自己的程序,一般在主程序AppDelegate.m中实现即可(上面那个函数也在这个文件里)

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}

需要注意的是,想要成功设置delegate还必须让AppDelegate支持NSUserNotificationCenterDelegate 协议,即在它的头文件里加上这个协议即可。

然后运行程序就OK了。

 

【转载】User notification 的实现方法

原文:http://www.cnblogs.com/streakingBird/p/4178466.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!