用户在AlertController上接受设置通知后,如果添加通知请求成功完成,我必须在选中的TableViewCell中设置UIButton。但即使代码到达那里,它也没有回应。我认为问题是,Handler Event的闭包与main函数的闭包不同,因为它在处理程序事件闭包中没有正确获取UIButton对象。我该如何解决这个问题?这是代码片段。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
var isAlarmSet=false
let center = UNUserNotificationCenter.current()
tableView.deselectRow(at: indexPath, animated: true)
if let alarmButton = tableView.cellForRow(at: indexPath) as? BroadcastViewCell{
var row=indexPath.row
isAlarmSet = alarmButton.notifyButton.isSelected
self.alarmButton=alarmButton.notifyButton
if !isAlarmSet
{
//Calculating the date variable for notification time in this section i dont put it in here because it is too long.
if (date?.timeIntervalSinceNow.sign == .minus)
{
let alertController=UIAlertController(title:"UYARI" , message: "Programın süresi geçmiştir.", preferredStyle: .alert )
let cancelAction = UIAlertAction(title: "Tamam", style: .cancel) { (action:UIAlertAction!) in
}
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
else
{
let alertController=UIAlertController(title:show.programName , message: "Program başlamadan 10 dakika önce uyarı alacaksınız.", preferredStyle: .alert )
let cancelAction = UIAlertAction(title: "İptal", style: .cancel) { (action:UIAlertAction!) in
}
let acceptAction=UIAlertAction(title: "Tamam" , style: .default, handler: { (action:UIAlertAction) in
if #available(iOS 10.0, *)
{
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: date!)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
//Set the request for the notification from the above
let request = UNNotificationRequest(
identifier: (show.airDate)!+(show.airTime)!,
content: content,
trigger: trigger
)
UNUserNotificationCenter.current().add(request)
{ (error:Error?) in
if error != nil
{
print(error?.localizedDescription)
return
}
print("Notification Register Success")
alarmButton.notifyButton.isSelected=true
UNUserNotificationCenter.current().getPendingNotificationRequests
{ (requests) in
self.notifications=requests
}
}
}
alertController.addAction(cancelAction)
alertController.addAction(acceptAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
}
答案 0 :(得分:1)
UI可以在主线程上更新 ,然后使用
DispatchQueue.main.async {
alarmButton.notifyButton.isSelected=true
}