如何在Swift 3中的另一个闭包内设置UIButton的isSelected属性
问题描述:
用户接受AlertController的设置通知后,如果添加通知请求成功完成,则必须在所选的TableViewCell内设置UIButton。但即使代码到达那里,它也没有回应。我认为问题在于,Handler事件的关闭与主函数的关闭不同,并且因为它在处理程序事件关闭内不能正确获取UIButton对象。我该如何解决这个问题?这是代码片。如何在Swift 3中的另一个闭包内设置UIButton的isSelected属性
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()重新加载?它不响应 – umutg4d
你是否尝试在'DispatchQueue.main.async {alarmButton.notifyButton.isSelected = true}中设置此属性' –