0

我在Swift中有一个Table View Controller项目。 我需要2个本地通知操作:一个完成并删除截止日期待办事项,另一个打开应用程序的视图控制器。 我已经为动作添加了NSNotificationCenter.defaultCenter().addObserver,但我仍然想知道如何删除cellForRowAtIndexPath中的待办事项。只使用通知操作。 我希望你能帮助我!先谢谢你!如何使本地通知操作直接删除Swift中的待办事项

这里是我的代码一些地方:

的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() 

ToDoTableViewController:

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

// 
let newIndexPath = NSIndexPath(forRow: todoItems.count, inSection: 0) 
todoItems.append(todoItem) 
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)   

//scheduleLocalNotification(todoItem) 
let notification:UILocalNotification = UILocalNotification() 
notification.category = "FIRST_CATEGORY" 
notification.alertBody = "Notification \(todoItem.title)" 
notification.fireDate = fixNotificationDate(todoItem.deadline) 
notification.userInfo = ["note":todoItem.note, "title": todoItem.title] 

     UIApplication.sharedApplication().scheduleLocalNotification(notification) 

回答

0

在你不及格application:handleActionWithIdentifier:forLocalNotification:completionHandler:之间的任何时刻和功能做一点事。因此这些功能不知道该怎么做。

您传递的identifier需要提供足够的信息来确定要执行的操作。例如,它可能是“删除= 123”删除记录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) 

此外,这些代码都不在cellForRowAtIndexPath

+0

我已经完成了!但我不知道如何让'func showAMessage()'从'indexPath'中删除** todoItems **。我已经尝试过这样的东西:'todoItems.removeAtIndex(indexPath.row)'就像'commitEditingStyle',但没有结果。 –