Timbo
Timbo

Reputation: 1214

Where should I put code that is supposed to fire AFTER the view has loaded?

I’m writing an objective-c program that does some calculations based on time and will eventually updates UILabels each second.

To explain the concept here’s some simplified code, which I’ve placed into the viewDidLoad of the class that handles the view.

- (void)viewDidLoad {

    [super viewDidLoad];
    // how do i make this stuff happen AFTER the view has loaded??
    int a = 1;
    while (a < 10) {
        NSLog(@"doing something");
        a = a + 1;
        sleep(1);
    }       
}

My problem is that the code halts the loading of the view until the loop is all complete (in this case 10 seconds).

Where should I put code that I want to fire AFTER the view has finished loading?

newbie question I know =/

Upvotes: 1

Views: 144

Answers (2)

drekka
drekka

Reputation: 21883

What you need to do is put the long running code on another thread. Then start this thread from the viewDidAppear method. Take a look at my response on another question. The code I put up does exactly what i think you need to look into. (and displays an busy indicator, but you can gut that part, just look at the threading and how it is started and how the thread tells the UI it has finished.

Upvotes: 1

iwasrobbed
iwasrobbed

Reputation: 46703

In viewDidAppear is probably your best bet.

By the way, sleep(1) isn't helping... that will make your app act like it's hung up in a calculation. Unless you are trying to delay it, remove that line of code.

If you want something like a countdown or countup timer... see this how to show countdown on uilabel in iphone?

Upvotes: 1

Related Questions