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',但沒有結果。 –

相關問題