How to update a UI label from a non UI thread in iOS

前端 未结 5 1514
情深已故
情深已故 2021-02-05 07:17

I am new to iOS development, I have plain objective -c class \"MoneyTimer.m\" for running timer, from there i want to update the an UI label with the changing value of timer.

5条回答
  •  感动是毒
    2021-02-05 07:47

    I've done something identical in the past.

    I used a function to set the label text:

    - (void)updateLabelText:(NSString *)newText {
        yourLabel.text = newText;
    }
    

    and then called this function on the main thread with performSelectorOnMainThread

    NSString* myText = @"new value";
    [self performSelectorOnMainThread:(@selector)updateLabelText withObject:myText waitUntilDone:NO];
    

提交回复
热议问题