Ios 如何使本地通知操作直接删除Swift中的待办事项

Ios 如何使本地通知操作直接删除Swift中的待办事项,ios,swift,uitableview,push-notification,uilocalnotification,Ios,Swift,Uitableview,Push Notification,Uilocalnotification,我在Swift中有一个表视图控制器项目。 我需要2个本地通知操作:一个完成并删除截止日期待办事项,另一个打开应用程序的视图控制器。 我已经为操作添加了NSNotificationCenter.defaultCenter().addObserver,但我仍然想知道如何从单元格中删除待办事项,以获得rowatinedexpath。仅使用通知操作。 我希望你能帮助我!提前谢谢你 以下是我的代码的一些部分: AppDelegate: func application(application: UIApp

我在Swift中有一个表视图控制器项目。 我需要2个本地通知操作:一个完成并删除截止日期待办事项,另一个打开应用程序的视图控制器。 我已经为操作添加了
NSNotificationCenter.defaultCenter().addObserver
,但我仍然想知道如何从
单元格中删除待办事项,以获得rowatinedexpath
。仅使用通知操作。 我希望你能帮助我!提前谢谢你

以下是我的代码的一些部分:

AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Actions
    var firstAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    firstAction.identifier = "FIRST_ACTION"
    firstAction.title = "Complete" // "First Action"

    firstAction.activationMode = UIUserNotificationActivationMode.Background
    firstAction.destructive = true
    firstAction.authenticationRequired = false

    var secondAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    secondAction.identifier = "SECOND_ACTION"
    secondAction.title = "Edit" // "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]

    firstCategory.setActions(defaultActions as! [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
    firstCategory.setActions(minimalActions as! [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Minimal)

    // NSSet of all our categories

    let categories:NSSet = NSSet(objects: firstCategory)



    let types:UIUserNotificationType = UIUserNotificationType(arrayLiteral: .Alert, .Badge)

    let mySettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as! Set<UIUserNotificationCategory>)

    UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)



func application(application: UIApplication!,
    handleActionWithIdentifier identifier:String!,
    forLocalNotification notification:UILocalNotification!,
    completionHandler: (() -> Void)!){


        if (identifier == "First_Action"){


            NSNotificationCenter.defaultCenter().postNotificationName("actionOnePressed", object: nil)




        }else  if (identifier == "Second_Action"){

            NSNotificationCenter.defaultCenter().postNotificationName("actionTwoPressed", object: nil)


        }


        completionHandler()

目前,您没有在
应用程序:handleActionWithIdentifier:forLocalNotification:completionHandler:
和执行某些操作的函数之间传递任何内容。因此,这些函数不知道该做什么

您传递的
标识符
需要提供足够的信息来确定要做什么。例如,删除记录123可能是“delete=123”。然后,您将在postNotification中传递此消息:

     NSNotificationCenter.defaultCenter().postNotificationName("actionTwoPressed",
         object: nil, userInfo: identifier)
现在,您希望在处理程序中为
actionTwoPressed
通知提取它,该通知是
showAMessage
,因此它需要一个参数:

    func showAMessage(notification: NSNotification) {
        // notification.userInfo is the identifier
    }
这也意味着您需要更改addObserver,包括“:”以显示传递的参数:

    NSNotificationCenter.defaultCenter().addObserver(self,
        selector: "showAMessage", name: "actionTwoPressed:", object: nil)

而且,这些代码都不会出现在
cellforrowatinexpath

中,我已经这样做了!但是我不知道如何使
func showAMessage()
indepath
中删除todoItems。我已经尝试过类似的方法:
todoItems.removeAtIndex(indexPath.row)
就像是对
committeeditingstyle
一样,但没有结果。
    NSNotificationCenter.defaultCenter().addObserver(self,
        selector: "showAMessage", name: "actionTwoPressed:", object: nil)