如何使本地通知操作直接删除 Swift 中的待办事项
Posted
技术标签:
【中文标题】如何使本地通知操作直接删除 Swift 中的待办事项【英文标题】:How to make local notification actions directly delete To-Do Items in Swift 【发布时间】:2016-02-11 23:32:01 【问题描述】:我在 Swift 中有一个 Table View Controller 项目。
我需要 2 个本地通知操作:一个用于完成和删除截止日期的待办事项,另一个用于打开应用程序的视图控制器。
我已经为操作添加了NSNotificationCenter.defaultCenter().addObserver
,但我仍然想知道如何从cellForRowAtIndexPath
中删除待办事项。仅使用通知操作。
我希望你能帮助我!提前谢谢!
以下是我的部分代码:
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)
【问题讨论】:
【参考方案1】:目前你没有在application:handleActionWithIdentifier:forLocalNotification:completionHandler:
和函数之间传递任何东西来做某事。因此这些函数不知道该做什么。
您传递的identifier
需要提供足够的信息来确定要做什么。例如,删除记录 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)
此外,这些代码都不会在cellForRowAtIndexPath
中。
【讨论】:
我已经做到了!但我不知道如何让func showAMessage()
删除indexPath
中的todoItems。我已经尝试过类似:todoItems.removeAtIndex(indexPath.row)
就像 commitEditingStyle
一样,但没有结果。以上是关于如何使本地通知操作直接删除 Swift 中的待办事项的主要内容,如果未能解决你的问题,请参考以下文章