我在我的应用程序中使用本教程来获取通知 http://www.appcoda.com/local-notifications-ios8/
每个通知都有操作 - 编辑。我在 viewDidLoad() 方法中为其添加了观察者:
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)
}
但是我如何向这个方法发送数据来知道哪个通知调用了这个方法呢?
最佳答案
声明您的通知
,如下所示,
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleModifyListNotification:", name: "modifyListNotification", object: nil)
在选择器
中使用冒号
。
和发布您的通知
,例如,
NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: "Your Object Value")
你的函数应该是,
func handleModifyListNotification(notification: NSNotification) {
NSLog("Object is %@", notification.valueForKey("object") as String!)
}
关于ios - 如何将数据从通知发送到应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28712454/