【问题标题】:How to set UIButton's isSelected property inside another closure in Swift 3如何在 Swift 3 的另一个闭包中设置 UIButton 的 isSelected 属性
【发布时间】:2018-01-10 15:39:41
【问题描述】:

在用户接受 AlertController 上的设置通知后,如果添加通知请求成功完成,我必须在选定的 TableViewCell 内设置 UIButton。但即使代码到达那里,它也不会对此做出响应。我认为问题是,处理程序事件的闭包与主函数的闭包不同,因此它没有在处理程序事件闭包内正确获取 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)
                }

            }
        }
    }

}

【问题讨论】:

  • 在事件发生后尝试重新加载数据
  • 你的意思是tableView.reloadData()吗?它也不响应
  • 您是否尝试在DispatchQueue.main.async { alarmButton.notifyButton.isSelected=true }中设置此属性
  • 当我把它放在 DispatchQueue.main.async 中时,它就像一个魅力,非常感谢你的帮助。 @Adrian Bobrowski

标签: ios swift swift3 uibutton


【解决方案1】:

UI 只能在主线程上更新,然后使用

DispatchQueue.main.async { 
    alarmButton.notifyButton.isSelected=true 
}

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 1970-01-01
    • 2019-11-08
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    相关资源
    最近更新 更多