Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

user interface - update text field ui in swift ios

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.

here is my code:

@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;
        }
    }
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...