ios - 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
?
解决方案
实际上,线程和调度队列是红鲱鱼。问题仅仅self.tryAgainButton
是对不在界面中的按钮的引用。它在思想空间的某个地方关闭。它没有超级视图并且不可见。因此,您调用removeFromSuperview
它并没有任何反应。
您确实向界面 ( reconnectButton
) 添加了一个按钮。[您以完全错误的方式这样做,但是您这样做的方式有什么问题将成为另一个问题的主题!] 但是您从未设置self.tryAgainButton
为reconnectButton
,因此它们不是相同的按钮。您有两个按钮,一个在界面中(reconnectButton
),一个在思想空间中(self.tryAgainButton
)。
推荐阅读
- python - 在 Python 中以 ASCII 格式打开二进制文件
- c# - 如何在.net核心中创建列表属性并在另一个没有ID的表中使用它?
- python - 找出哪些样本在整数池中
- python - 正则表达式和具有多个函数和 f 字符串的列表理解的问题
- javascript - React native push 不是一个函数
- python - Python - 如何仅提取字典中的列表名称
- reactjs - 如何从父反应钩子打开对话框材质ui
- github - 如何在 pull_request_review 中使用类似于 pull_request_target 的秘密?
- c++ - 为什么 std::next_permutation 会跳过一些排列?
- python - 获取控制台输出 - Tkinter