我将本教程中的通知用于我的应用程序http://www.appcoda.com/local-notifications-ios8/
每个通知都有操作-编辑。我在viewDidLoad()方法中为其添加了Observer:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleModifyListNotification", name: "modifyListNotification", object: nil)
当按下通知中的编辑按钮时,应用会调用此方法:
func handleModifyListNotification() {
}
用于分配通知,我使用:
func scheduleLocalNotification() {
var dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2015;
dateComp.month = 02;
dateComp.day = 24;
dateComp.hour = 14;
dateComp.minute = 34;
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calender:NSCalendar? = NSCalendar(calendarIdentifier: NSGregorianCalendar)
var date:NSDate = calender!.dateFromComponents(dateComp)!
var localNotification = UILocalNotification()
localNotification.fireDate = fixNotificationDate(date)
localNotification.alertBody = "Hey, you must go shopping, remember?"
localNotification.alertAction = "View List"
localNotification.category = "shoppingListReminderCategory"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
但是,如何将数据发送到此方法以知道哪个通知调用此方法?
声明您notification
喜欢的关注者,
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleModifyListNotification:", name: "modifyListNotification", object: nil)
同colon
在Selector
。
和Post your notification
喜欢,
NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: "Your Object Value")
而你的职能应该是
func handleModifyListNotification(notification: NSNotification) {
NSLog("Object is %@", notification.valueForKey("object") as String!)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句