首页 > 解决方案 > UI is not getting update although using main thread

问题描述

I am using Xcode and Swift. I have a class for the UIViewController I am using. On this UIViewController I want to present some kind of pop over view with my custom class ConnectionLostView. On this UIView there is an UIButton. If you press the button the tryToReconnect() function is called (which works). This function processes the online data (which works too) and should update my UI using DispatchQueue.main.async { //updating UI } but my UI isn't getting updated (or rather I can't i.e. remove my button from its superview but I can remove self (what exactly works and what doesn't you can see as comment in the code below))

That is the class of the UIViewController I am using to present my view.

class vc: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let connectionStatusView = ConnectionLostView()
        connectionStatusView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(connectionStatusView)

        //setting up the constraints for connectionStatusView
    }
}

This is the class of my UIView:

class ConnectionLostView: UIView {
    let tryAgainButton = UIButton(type: .roundedRect)

    func tryToReconnect() {
        let url = URL(string: "http://worldclockapi.com/api/json/est/now")!
        let config = URLSessionConfiguration.default
        let session = URLSession(configuration: config)
        let task = session.dataTask(with: url) { (data, response, error) in
            if error != nil {
                print(error)
            } else {
                do {
                    if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] {
                        if let data = json["data"] as? String {
                            // code for processing the data

                            DispatchQueue.main.async {
                                self.removeFromSuperview() //Does work
                                self.tryAgainButton.removeFromSuperview() // does not work
                            }
                        }
                    }
                } catch {
                    print(error)
                }
            }
        }
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)

        //setting up the button
        let buttonAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18)]
        let attributedButtonString = NSAttributedString(string: "Try To Reconnect", attributes: buttonAttributes)
        let reconnectButton = UIButton(type: .roundedRect)
        reconnectButton.translatesAutoresizingMaskIntoConstraints = false
        reconnectButton.setAttributedTitle(attributedButtonString, for: .normal)
        reconnectButton.addTarget(self, action: #selector(tryToReconnect), for: .touchUpInside)
        addSubview(reconnectButton)

        // setting up constraints for reconnectButton
    }
}

How can I fix my code so the UI is updated when I press the reconnectButton?

标签: iosswiftuser-interfaceurlsessionnetworkonmainthread

解决方案


实际上,线程和调度队列是红鲱鱼。问题仅仅self.tryAgainButton是对不在界面中的按钮的引用。它在思想空间的某个地方关闭。它没有超级视图并且不可见。因此,您调用removeFromSuperview它并没有任何反应。

您确实向界面 ( reconnectButton) 添加了一个按钮。[您以完全错误的方式这样做,但是您这样做的方式有什么问题将成为另一个问题的主题!] 但是您从未设置self.tryAgainButtonreconnectButton,因此它们不是相同的按钮。您有两个按钮,一个在界面中(reconnectButton),一个在思想空间中(self.tryAgainButton)。


推荐阅读