Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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. I want to Know how to access the UI element from non UI thread ? I am Using Xcode 4.2 and storyboarding.

In blackberry simply by getting the event lock one can update the UI's from non UI thread.

//this the code from MyTimerClass

 {...
    if(nsTimerUp == nil){

        nsTimerUp = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(countUpH) userInfo:nil repeats: YES];
 ...}

(void) countUpH {

sumUp = sumUp + rateInSecH;
 **//from here i want to update the UI label **
...
}
like image 741
Jitendra kumar jha Avatar asked Jun 28 '12 10:06

Jitendra kumar jha


People also ask

How do I change the UILabel text in Swift?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.

What is UI label in Swift?

A view that displays one or more lines of informational text.


1 Answers

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

like image 64
shoughton123 Avatar answered Oct 29 '22 15:10

shoughton123