在向我的闹钟应用程序实现通知时遇到问题



我创建了一个闹钟应用程序,看起来类似于ios闹钟应用程序,单元格和表格视图看起来也与它相似。单元格有 2 件事标签和切换按钮,因此当我打开按钮时,警报会在单元格的标签时间激活。

有一个triggerAlarm()函数,它跟踪时间并循环遍历[Item]()类型的items数组(Item是保存数据的NSManagedObject类型的一类。它有 2 个属性time: String&isOn: Bool(。

问题是viewDidLoad()中的Timer方法没有在 func 中通过该循环triggerAlarm()而是打印realTime。循环应该将realTime与 items 数组进行比较,如果isOn属性为 true,则调用notificationTrigger()func,但它没有发生。

Xcode也没有显示任何错误。

这是代码:

import UIKit
import UserNotifications
class TableViewController: UITableViewController {
var realTime  = String()
var items     = [Item]()
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in})
}
override func viewWillAppear(_ animated: Bool) {
getData()
tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! TableViewCell
let row = items[indexPath.row]
cell.timeLbl.text   = row.time
cell.switchBtn.isOn = row.isOn
cell.callback = { newValue in
row.isOn = newValue
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
return cell
}
func getData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
items = try context.fetch(Item.fetchRequest())
}catch{
print("(error)")
}
}
func triggerAlarm() {
realTime = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .short)
print(realTime)
for i in 0..<items.count {
if (items[i].time!) == realTime && items[i].isOn == true{
print(time)
self.notificationTrigger()
}else {
return
}
}
}
func notificationTrigger() {
let content      = UNMutableNotificationContent()
content.title    = "time is up (time)"
content.subtitle = "asdf"
content.body     = "qwer"
content.badge    = 0
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "customNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
@objc func triggerAlarm() {
realTime = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .short)
print(realTime)
for i in 0..<items.count {
if (items[i].time!) == realTime && items[i].isOn == true{
print(time)
self.notificationTrigger()
}else {
return
}
}
}

在这里,您使用的是 return in else。因此,它不是打破当前迭代的循环(我想你想实现这一点(,而是从整个方法调用返回。而是使用:-

继续

或者最好省略其他部分

@objc func triggerAlarm() {
realTime = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .short)
print(realTime)
for i in 0..<items.count {
if (items[i].time!) == realTime && items[i].isOn == true{
print(time)
self.notificationTrigger()
}
}
}

最新更新