ios中更新文本字段ui

ios中更新文本字段ui

本文介绍了在swift ios中更新文本字段ui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用swift的ios SDK 8时,我遇到了一个问题

i meet a question when i'm using swift of ios SDK 8

我使用回调来获取数据背景,当我获取数据时,我想设置一个文本字段值和更新屏幕。

I used a callback to fetch data background and when data fetched, i want to set a text field value and update screen.

这是我的代码:

@IBOutlet var txtTest: UITextField!

@IBAction func login() {
    let username = "aaa";
    let password = "bbb";
    var res = ServiceClient.login(username, password: password){
        (res:String, error:String?) -> Void in if(error == nil){
            self.txtTest.text = res;
        }
    }
}

运行此代码和数据正确获取并且txtTest没有更新,但是当我点击txtTest时,将显示该值。那么有没有办法强制更新UI?或者向UI线程发送消息?

Run this code and the data is correctly fetched and the txtTest did not updated, but when i tap txtTest, the value will be shown. So is there any way to force update UI? or send a message to UI thread?

推荐答案

当想要在Swift中进行UI更新时,每个人都有同样的问题:不要永远更新任何辅助线程中的UI。这意味着任何有封闭的东西。由于Swift使用了很多闭包,所以这个问题看起来要多得多,但并不是新的。

Same issue that everyone is having when wanting to do UI updates in Swift: do not ever update the UI in any secondary thread. And that means anything with closures. Since Swift is using closures so much, that issue is being seen a lot more but it isn't new.

参见才能正确完成工作。

See Swift Update Label (with HTML content) takes 1min for the correct way of doing things.

这篇关于在swift ios中更新文本字段ui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 17:26