iPhone开发 Swift - NSNotification 通知
Swift创建Notification通知
- 创建一个SingleView Application
- 打开AppDelegate.swift,在方法
application(application:UIApplication,didFinishLaunchingWithOptions launchOptions: NSDictionary?)
中输入代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
//设置Notification 的类型
let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge
//设置Notification的设置项,其中categories参数用来设置Notification的类别
let mySettings: UIUserNotificationSettings = UIUserNotificationSettings(forType: types, categories: nil);
//注册UserNotification
UIApplication.sharedApplication().registerUserNotifiationSettings(mySettings)
return true
}
- 配置不同的Actions,在方法
application(application:UIApplication,didFinishLaunchingWithOptions launchOptions: NSDictionary?)内的代码开始部分输入以下代码
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
//define Actions
var firstAction: UIMutableUserNotificationAction = UIMutableUserNotificationAction();
// The unique identifier for this action
fistAction.identifier = “FIRST_ACTION”;
// The localized title to display for this action
firstAction.title = “First Action”;
//define action’s activationMode, // How the application should be activated in response to the action
firstAction.activationMode = UIUserNotificationActivationMode.Background// 当点击的时候不启动程序,在后台处理
//define action’s destructive // Whether this action should be indicated as destructive when displayed.
firstAction.destructive = true
//define authentication // Whether this action is secure and should require unlocking before being performed. If the activation mode is UIUserNotificationActivationModeForeground, then the action is considered secure and this property is ignored.
firstAction.authenticationRequired = false//不需要用户解锁手机即可以处理该Notification
var secondAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
secondAction.identifier = "SECOND_ACTION";
secondAction.title = "Second Action";
secondAction.activationMode = UIUserNotificationActivationMode.Foreground
secondAction.destructive = false
secondAction.authenticationRequired = false
var thirdAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
thirdAction.identifier = "THIRD_ACTION";
thirdAction.title = "Third Action";
thirdAction.activationMode = UIUserNotificationActivationMode.Background
thirdAction.destructive = false
thirdAction.authenticationRequired = false
//Category
var firstCategory: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
firstCategory.identifier = "FIRST_CATEGORY";
let defaultActions:NSArray = [firstAction, secondAction, thirdAction];
let minimalActions:NSArray = [firstAction, secondAction];
// Sets the UIUserNotificationActions in the order to be displayed for the specified context
firstCategory.setActions(defaultActions, forContext: UIUserNotificationActionContext.Default);// // the default context of a notification action
firstCategory.setActions(minimalActions, forContext: UIUserNotificationActionContext.Minimal);//Minimal // the context of a notification action when space is limited
let categories: NSSet = NSSet(objects: firstCategory)
//设置Notification 的类型
let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge
//设置Notification的设置项,其中categories参数用来设置Notification的类别
let mySettings: UIUserNotificationSettings = UIUserNotificationSettings(forType: types, categories: nil);
//注册UserNotification
UIApplication.sharedApplication().registerUserNotifiationSettings(mySettings)
return true
}
4. 打开ViewController.swift文件,在ViewDidLoad方法中输入以下代码
override func viewDidLoad() {
super.viewDidLoad()
//define notification center
NSNotificationCenter.defaultCenter().addObserver(self, selector: “TestShape:”, name: “actionOne”, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:”TestMessage:”, name: “actionTwo”, object: nil)
//定义一个触发Notification的程序
var dateComp: NSDateComponents =
NSDateComponents()
dateComp.year = 2014
dateComp.month = 09
dateComp.day = 09
dateComp.hour = 11
dateComp.minute = 11
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calendar: NSCalendar = NSCalendar(calendarIdentifier:
NSGregorianCalendar)
var date: NSDate = calendar.dateFromComponents(dateComp)
//define Location notification
var notification: UILocalNotification =
UILocalNotification()
notification.category = “FIRST_CATEGORY”;
notification.alertBody = “This is a notification”
notification.fireDate = date
//fire notification
UIApplication.shareApplication().scheduleLocalNotification(notification)
}
func TestShape(notification: NSNotification) {
UIView *view = UIView(frame:
CGRectMake(100,100,100,100));
view.backgroundColor = UIColor.blackColor()
Self.view.addSubview(view)
}
func TestMessage(notification: NSNotification) {
var message:
UIAlertController = UIAlertController(title: “Notification Message”, message: “Hello,
this is an alert message”, preferredStyle: UIAlertControllerStyle.Alert)
message.addAction(UIAlertAction(title: “OK”, style:
UIAlertActionStyle.Default, handle: nil))
self.presentViewController(message, animated: true,
completion: nil)
}
5. 回到AppDelegate.swift,并添加以下方法
func
application(application: UIApplication!, handleActionWithIdentifier identifier: String!,
forLocalNotification notification: UILocalNotification!, completionHandler: (() -> Void)!) {
if identifier ==
“FIRST_ACTION” {
NSNotificationCenter.defaultCenter().postNotificationName(“actionOne”,
object: nil)
}
else if identifier == “SECOND_ACTION” {
NSNotificationCenter.defaultCenter().postNotificationName(“actionTwo”,
object: nil)
}
completionHandler()
}
iPhone开发 Swift - NSNotification 通知的更多相关文章
- iphone开发 IOS 组织架构图
转载自 :http://blog.csdn.net/mashi321323/article/details/18267719 登录|注册 mashi321323的专栏 目录视图 ...
- iPhone开发视频教程 Objective-C部分 (51课时)
第一.二章 OC基础语法 iPhone开发教程 第一章 OC基础语法 iPhone开发概述-必看(1.1)http://www.apkbus.com/android-102215-1-1.html ...
- iPhone开发常问的十个问题
iPhone开发常问的十个问题 前言 今天去stackoverflow.com上看了一下iPhone标签下排名最高的10个问题,将它们整理出来,希望这些常见问题能帮到一些iPhone开发的初学者.本来 ...
- iPad开发(相对于iPhone开发时专有的API)
iPad开发 一.iPad开发简介 1.什么是iPad 一款苹果公司于2010年发布的平板电脑 定价介于苹果的智能手机iPhone和笔记本电脑产品之间 跟iPhone一样,搭载的是iOS操作系统 2. ...
- (转载)iPhone开发视频教程 Objective-C部分 (51课时)
感谢好人的无私贡献!来源:http://www.cnblogs.com/aimeng/p/3370012.html 第一.二章 OC基础语法 iPhone开发教程 第一章 OC基础语法 i ...
- iphone开发技术要学习的内容
一.iOS基础 1 开发环境搭建以及IOS组件.框架的概要介绍. 2 mac操作系统与iOS操作系统 3 xcode IDE开发环境的初始 二.C语言基础 1数据类型.表达式与控制流程语句 2数组.函 ...
- iPhone开发视频教程 Objective-C部分
第一.二章 OC基础语法 iPhone开发教程 第一章 OC基础语法 iPhone开发概述-必看 (1.1) http://www.apkbus.com/android-102215-1-1.ht ...
- iPhone开发与cocos2d 经验谈
转CSDN jilongliang : 首先,对于一个完全没有mac开发经验,甚至从没摸过苹果系统的开发人员来说,首先就是要熟悉apple的那一套开发框架(含开发环境IDE.开发框架uikit,还有开 ...
- iOS开发UI篇—iPad和iPhone开发的比较
一.iPad简介 1.什么是iPad 一款苹果公司于2010年发布的平板电脑 定位介于苹果的智能手机iPhone和笔记本电脑产品之间 跟iPhone一样,搭载的是iOS操作系统 2.iPad的市场情况 ...
随机推荐
- jsonUtil 工具类
package org.konghao.basic.util; import java.io.IOException; import java.io.StringWriter; import com. ...
- 详解MyEclipse10 安装Spket 1.6.23(支持Extjs4.1.1及jQuery1.8)
用MyEclipse10安装Spket主要有3种方式:在线下载更新.下载Zip覆盖.下载jar包安装.我用在线安装尝试了N次终于还是失败,只好下载jar包来安装,在失败了M次之后终于安装成功,现在网上 ...
- Python抓取页面中超链接(URL)的三中方法比较(HTMLParser、pyquery、正则表达式) <转>
Python抓取页面中超链接(URL)的3中方法比较(HTMLParser.pyquery.正则表达式) HTMLParser版: #!/usr/bin/python # -*- coding: UT ...
- js设置控件的隐藏与显示的两种方法
js设置控件的隐藏与显示,设置控件style的display和visibility属性就可以了,下面有个示例,需要的朋友可以参考下用JavaScript隐藏控件的方法有两种,分别是通过设置控件的sty ...
- cdoj 1141 酱神寻宝 状压dp
酱神寻宝 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1141 Descri ...
- hdu 5268 ZYB loves Score 水题
ZYB loves Score Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- C++ Code_HotKey
Code::使用HotKeyCtrl定义一个系统热键 // 关联HotKeyCtrl控件变量 m_HotKey1 BEGIN_MESSAGE_MAP(CXyzDlg, CD ...
- 【机器学习算法-python实现】决策树-Decision tree(1) 信息熵划分数据集
(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 决策书算法是一种逼近离散数值的分类算法,思路比較简单,并且准确率较高.国际权威的学术组织,数据挖掘国际 ...
- AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字,Filterable的使用)
AutoCompleteTextView具有输入提示的功能,但是它的这种提示不适合对股票列表的过滤,如果你玩过股票软件,就会知道只要输入股票名称的首字母或股票代码就会出现符合匹配的股票,这种过滤怎么实 ...
- Q_INVOKABLE与invokeMethod用法全解
在Qt/Qt Quick宏浅议一文中,我们将介绍Qt中经常使用的几个宏: Q_OBJECT, SIGNAL与SLOT, Q_SIGNALS 与 Q_SLOTS, Q_EMIT ,Q_INVOKABLE ...