Ios 如何在Swift 3中的另一个闭包内设置UIButton的isSelected属性
在用户接受AlertController上的设置通知后,如果添加通知请求成功完成,我必须在选定的TableViewCell内设置UIButton。但即使代码到达了那里,它也没有响应。我认为问题在于,处理程序事件的闭包不同于主函数的闭包,因此它无法在处理程序事件闭包中正确获取UIButton对象。我如何处理这个问题?这是代码段Ios 如何在Swift 3中的另一个闭包内设置UIButton的isSelected属性,ios,swift,swift3,uibutton,Ios,Swift,Swift3,Uibutton,在用户接受AlertController上的设置通知后,如果添加通知请求成功完成,我必须在选定的TableViewCell内设置UIButton。但即使代码到达了那里,它也没有响应。我认为问题在于,处理程序事件的闭包不同于主函数的闭包,因此它无法在处理程序事件闭包中正确获取UIButton对象。我如何处理这个问题?这是代码段 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { va
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)
}
}
}
}
}
UI只能在主线程上更新,然后使用
DispatchQueue.main.async {
alarmButton.notifyButton.isSelected=true
}
在事件发生后尝试重新加载数据是指tableView.reloadData吗?它没有响应。您是否尝试在DispatchQueue.main.async{alarmButton.notifyButton.isSelected=true}中设置此属性?当我将其放入DispatchQueue.main.async中时,它非常有用,非常感谢您的帮助@阿德里安·博布朗斯基