如何将数据从通知发送到应用程序

时间:2015-02-25 06:17:14

标签: ios swift notifications

我将此教程用于我的应用http://www.appcoda.com/local-notifications-ios8/

中的通知

每个通知都有操作 - 编辑。我在viewDidLoad()方法中添加了Observer:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleModifyListNotification", name: "modifyListNotification", object: nil)

当按下通知中的编辑按钮时,app会调用此方法:

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)
    }

但是如何将数据发送到此方法以了解哪种通知调用此方法?

1 个答案:

答案 0 :(得分:2)

声明你的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!)
}