views:

79

answers:

2

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 =/

+1  A: 

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 http://stackoverflow.com/questions/1525124/how-to-show-countdown-on-uilabel-in-iphone

iWasRobbed
I'll give that a go thanks. I am trying to delay it, it's a part of the main game loop to run a particular process each second and update various controls
Timbo
Timbo, instead of using sleep(1); though, you probably want to look into using an NSTimer like in the example link I posted. That will call a method at whatever interval you specify, say every second without delaying everything else in your app.
iWasRobbed
+1 to Rob. NSTimer is exactly what you want to fire a particular event every second (provided it doesn't have to be millisecond accurate, which I guess it doesn't since you are currently using sleep). You could also use a separate thread as Derek says, but that gets you into issues of thread synchronisation.
JeremyP
Problem is that I don't want to fire 1 message on a schedule, I want to fire a whole page of code, my example is simplified to explain just the concept. Leaving the sleep in the code the viewDidAppear still has the same problem, if I remove the sleep the page of code flys by too fast
Timbo
+1  A: 

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.

Derek Clarkson
I'm still coming to grips with obj-c so thanks for your patience all. I now understand that sleep(1) is not an option as it halts viewDidAppear from appearing. While I can now use NSTimer to happily call a method repeatedly or just once, my issue now is that the this method can't access class instances I set up in viewDidAppear. Unfortunately the method NSTimer calls needs to use class instances that are already set up so that it knows when to invalidate the NSTimer and stop the loop.I realise this knowledge gap is fundamental, I'm just notorious for biting of more than I can chew ;-)
Timbo