Looping, but not updating

Discussion in 'iOS Programming' started by cellulit, Nov 5, 2011.

  1. cellulit macrumors newbie

    Joined:
    Nov 5, 2011
    #1
    Hello everybody;
    My first post and first steps in iOS programming. Thanks in advance for help.
    Probably something basic...

    One the screen one button and one label.
    The idea is to generate a random number from 0 to 10, upon pressing the button and to keep going until the number is bigger than 5 (just for example). Every time the number is generated, I want to update the label with it.

    My controller is damn simple and has one "while" loop inside. The issue is, it won't update the label until it meets the condition. Even though inside the loop I am trying to update the text with the most recent value.

    It works fine if I remove the loop and execute everything once, upon pressing the button.
    What am I not getting?
    Thanks.
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    You're in an event-driven environment.

    When you update the label, that'll just record that the label needs to be redisplayed, it won't actually redraw the label then and there. It's only later when it comes to process the event to update the screen that the label will redrawn. But your while loop is blocking event processing from dealing with the screen update events. When you finally stop block blocking event processing, the screen update event processes, but by then the label has been set to the final value. That is why you only see the last value.

    If you're just learning starting to learn iOS programming, forget about this for now. It sounds like an easy problem, but it's complicated by events. There more important things for you learn for now, than how to break up a traditional while loop.
     
  3. cellulit thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #3
    Thanks a lot. That explains the situation.
    Still I'd like to at least start understanding it better. Is this threads? Timers?
     
  4. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #4
    Yes. NSTimers is one technique. Just exit your method after setting a timer for the next label update, instead of trying to loop and wait. The OS will redraw the label between your methods.
     
  5. cocell, Nov 6, 2011
    Last edited: Nov 6, 2011

    cocell macrumors newbie

    Joined:
    Nov 4, 2011
    Location:
    Los Angeles,CA
    #5
    You can try this.

    Code:
    Numbers.h
    @interface Numbers:UIViewController{
    
    UILabel IBOutlet *myLabel;
    NSTimer *myTimer;
    }
    -(IBAction)myButton:(id)sender;
    -(void)myRandom;
    @end
    
    
    
    Numbers.m
    
    @implementaion Numbers
    
    -(IBAction)myButton:(id)sender{
        
                 myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self 
    selector:@selector(myRandom) userInfo:nil repeats:YES];
        
    }
    
    -(void)myRandom{
        static int counter;
        
        if (counter != 5) {
    
        NSString *myString = [NSString stringWithFormat:@"%i",rand() % 10];  
            myLabel.text = myString;
            counter++;
            
        }else{
            [myTimer invalidate];
            counter = 0;
        }
           
    }
     
  6. cellulit thread starter macrumors newbie

    Joined:
    Nov 5, 2011
    #6
    Guys;
    Thanks for all the help. I'm slowly incorporating it to my simple code now. Appreciated!
     

Share This Page