Resolved Adding values from an NSArray to a UILabel

Discussion in 'iOS Programming' started by fstigre, Nov 11, 2012.

  1. fstigre, Nov 11, 2012
    Last edited: Nov 13, 2012

    fstigre macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #1
    Hi,

    I'm trying to display the values from an NSArray and for some reason I'm only getting the last value.

    In this code I have to NSNumbers in an NSArray but when I try to out put their values I only get the value from the last object in the array.

    Any idea what am I doing wrong?

    Code:
    [CODE]    NSNumber *myInt;
        myInt = [NSNumber numberWithInt:5];
        
        NSNumber *myInt2;
        myInt2 = [NSNumber numberWithInt:10];
        
        NSArray *numbers =  [NSArray arrayWithObjects:myInt, myInt2, nil];
        
        for(int i = 0; i < [numbers count]; i++)
        {
            int nums = [[numbers objectAtIndex:i] intValue];
            self.display.text = [NSString stringWithFormat:@"%i \n", nums];        
        }
    [/CODE]


    Out putting the values to the console is not a problem
    Code:
     NSLog(@"%@ \n", [numbers objectAtIndex:i]);
     
  2. ArtOfWarfare, Nov 11, 2012
    Last edited: Nov 11, 2012

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Each time you assign a new string to the text field, you discard the prior string that was in the text field.

    Have a look at NSMutableString's appendFormat: method.

    Edit:

    And as a side note, you might want to change your for loop to a for-each... I recently read something that said that, in addition to being more readable, it's quicker than a normal for loop.

    Code:
    for (NSNumber* number in numbers) {
        int num = [number intValue];
        [string appendFormat: ... ]; // <== Your code in the ...
    }
    You'll also need to add some code before and after the for-each loop... I had written it in but I decided I was telling you to much and you needed to figure out more of it for yourself, so I took it out.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    The screen doesn't update immediately when you set the text property on a label. Drawing in iOS happens when your code returns to the system code. Since that only happens when your function is complete only the last value for the text property is shown on screen.

    One way to implement this kind of display would be to use an NSTimer. Creating a repeating timer. Each time the timer calls back update the label. When you've reached the last value then invalidate the timer.
     
  4. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #4
    Thank you all for your help, I will work on what you have sugested to make this work.

    Thanks a lot!
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    What do you want it to do? You've stated what it actually does, but haven't described what you expect to see. This will help us in suggesting approaches to a solution.
     
  6. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #6
    What I’m trying to do is simply display all of the items in the NSArray on the UILabel.

    I'm still trying to figure this out :(.

    Thanks
     
  7. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #7
    you should replace

    Code:
    for(int i = 0; i < [numbers count]; i++)
        {
            int nums = [[numbers objectAtIndex:i] intValue];
            self.display.text = [NSString stringWithFormat:@"%i \n", nums];        
        }
    
    with something like this

    Code:
    NSString*string = @"";
    
    for(int i = 0; i < [numbers count]; i++)
        {
            int nums = [[numbers objectAtIndex:i] intValue];
            string = [string stringByAppendingFormat:@"%i \n", nums];        
        }
    
    self.display.text = string;
    
    notice: i did not check over my syntax, so check it yourself
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Still too vague, really. Do you want the values comma-separated, space-separated, one value per line (assuming your UILabel is multi-line), something else?
     
  9. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #9
    Thank you all for your reply.

    @waterskier2007
    Will try what you suggested as soon as I can and let you know.

    @dejo
    I want every value in a separate line. Sorry, I didn't provide this detail of information because I think I can make that part work by my own.

    Thank you all a lot.
     
  10. fstigre, Nov 12, 2012
    Last edited: Nov 12, 2012

    fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008
    #10
    @waterskier2007

    Your code worked, thanks a lot.

    The only thing is not working is the new line break (\n), I thought this was going to work but for some reason it didn't, I will figure this out by my own.

    EDIT:
    For the line break I used the new line operator and (\n) and set the numberOfLInes to 0...
    Final code.
    Thank you all very much.
     
  11. fstigre thread starter macrumors regular

    fstigre

    Joined:
    Aug 12, 2008

Share This Page