ios – 如何在swift中取消特定的UILocalNotification?

ios – 如何在swift中取消特定的UILocalNotification?,第1张

概述我在表视图中有一个单元格,其中有一个用于安排通知的按钮.是否可以删除或取消特定单元格的通知而不影响其他单元格安排的通知?我想使用相同的按钮删除或取消特定通知. 如果是这样,请帮我提供一个示例快速代码. @IBAction func setNotification(sender: UIButton!) { cell = tableView.cellForRowAtIndexPat 我在表视图中有一个单元格,其中有一个用于安排通知的按钮.是否可以删除或取消该特定单元格的通知而不影响其他单元格安排的通知?我想使用相同的按钮删除或取消特定通知.
如果是这样,请帮我提供一个示例快速代码.

@IBAction func setNotification(sender: UIbutton!) {        cell = tableVIEw.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag,inSection: 0)) as! CustomtableVIEwCell        if sender.selected {            sender.selected = false             }else {currentRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude: latitude,longitude:longitude),radius: radius,IDentifIEr: IDentifIEr)regionMonitor()            manager.stopUpdatingLocation()            sender.selected = true } }

dIDEnterRegion()的代码

func locationManager(manager: CLLocationManager!,dIDEnterRegion region: CLRegion!) {            localnotification.regionTriggersOnce = true            localnotification.alertbody = "you have reached"            UIApplication.sharedApplication().schedulelocalnotification(localnotification)            NSLog("Entering region") }
解决方法 您可以在userInfo中为每个UIlocalnotification保存唯一标识符,然后遍历它们以查找唯一ID并将其删除:

var app:UIApplication = UIApplication.shared    if let localnotifications = app.scheduledlocalnotifications {        for oneEvent in localnotifications {            var notification = oneEvent as UIlocalnotification            if let userInfoCurrent = notification.userInfo as? [String:AnyObject],let uID = userInfoCurrent["uID"] as? String {                if uID == uIDtodelete {                    //Cancelling local notification                    app.cancellocalnotification(notification)                    break                }            }        }    }

更新为Swift 3并进行了改进以防止无崩溃

总结

以上是内存溢出为你收集整理的ios – 如何在swift中取消特定的UILocalNotification?全部内容,希望文章能够帮你解决ios – 如何在swift中取消特定的UILocalNotification?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1008035.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存