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

前端 未结 5 1438
情深已故
情深已故 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:54

    This is the quickest and simplest way to do it is:

    - (void) countUpH{
    
       sumUp = sumUp + rateInSecH;
       //Accessing UI Thread
       [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    
          //Do any updates to your label here
          yourLabel.text = newText;
    
       }];
    }
    

    If you do it this way you don't have to switch to a different method.

    Hope this helps.

    Sam

提交回复
热议问题