[Help Needed] UILabel Integer thingy

Discussion in 'iOS Development' started by cocotutch, Aug 27, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I have a UILabel which I can display a correctly formatted Integer. In IB, I have "000" as the starting text, when I launch my app, I want the UILabel to cycle from "000" to "999" very quickly (as in 001,002,003,004 etc) and loop on until the user hits the "STOP!" button which will stop this in its tracks. I have sorta nutted this out, i.e. with a tiny delay in between the number settings, but I know there is a MUCH easier way. Care to help?

    Sorry for all the questions here,

    cocotutch
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    There are many, many, many, many ways to do this. Take recursion or timers for example.

    - viewDidAppear: {
    [super viewdidAppear:sure];
    [self tick];
    }

    - tick {
    label.text = [NSString stringWithFormat@"%d", ++[label.text integerValue]];
    if (label.text < 1000)
    [self performSelector

    Please Register or Log in to view images

    selector(tick) afterDelay:delay];
    }




    OR

    - viewdidAppear {
    [superViewdidAppear];
    [self scheduleATimerWithInterval:etc:];
    }

    - tick {
    [self doWhatIWroteInTheLastExampel];
  3. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I get an error with that StringWithFormat;

    error: expected ']' before 'OBJC_STRING' token
    warning: 'NSString' may not respond to '+stringWithFormat'.

    Its not in the Docs..........

    Please Register or Log in to view images



    My integer is called "currentVal" so I put that anywhere? This is entirely new to me.

    EDIT: You forgot a ":" between stringWithFormat and @"%d", I added that, but then it gives me another error:

    lvalue required as increment operand
  4. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    This would work:

    [OBJC]
    int temp = [myLabel.text integerValue];
    myLabel.text = [NSString stringWithFormat

    Please Register or Log in to view images

    "%d", ++temp];
    [/OBJC]


    EDIT: Did some thinking:

    I'm pretty sure that the int needs to be previously declared, because even something as simple as
    Code:
    NSLog(@"%d", ++2);
    throws a compiler error. Methinks the gcc has gotten stricter with this sort of thing.


    Also, here's a quick reference on the increment/decrement operators:

  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    My quickly and poorly written pseudocode was supposed to be taken as an "idea," not as a steadfast "copy and paste solution." That's why I put absolutely no effort into making legitimate code.

    Honestly, I'm disappointed in you. Very disappointed.

    For the stringWithFormat line not being in the docs, look harder. At the NSString documentation. It's there.

    For stringWithFormat@"string" causing errors, take a look at what is actually written. There's no semicolon.

    The ++[string integerValue] won't work at all. You're supposed to get the value and increment it. Since you're keeping a global int for your count (why?), then just simply use ++currentVal.



    Axis, you can't increment or decrement a constant. You've never been able to. 2 is not a variable, it's a constant.

Share This Page