Timer question

Discussion in 'iOS Programming' started by Poy77, Jun 12, 2011.

  1. Poy77, Jun 12, 2011
    Last edited by a moderator: Jun 12, 2011

    Poy77 macrumors newbie

    Joined:
    Sep 5, 2010
    #1
    I am using Timer to get alarm after a while and I took example from apple source code:
    Code:
    CADisplayLink *aDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame)];
            [aDisplayLink setFrameInterval:animationFrameInterval];
            [aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            self.displayLink = aDisplayLink;
    It is working, but I a got warning:
    displayLinkWithTarget:selector method not found.

    Also if I set lock screen, the timer won't work. What I should use instead of it?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Odd. It's a documented method.

    Have you imported the header for CADisplayLink in the .m file for you're class?
     
  3. Shawnpk macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #3
    Do you have a drawFrame method?
     
  4. Poy77 thread starter macrumors newbie

    Joined:
    Sep 5, 2010
    #4
    No, I use my own (previous was just copied from apple code):
    displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:mad:selector(decreaseTimer:)];

    And I have decreaseTimer method.

    Why timer doesn't work while iphone is inactive?
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    You get a warning on that code because you're using NSClassFromString(). That means the compiler can't tell what the actual class will be at runtime, so it can't tell whether it has the given method or not.

    Is there some reason you're using NSClassFromString()? It seems like an odd thing to do. If you require the CADisplayLink class anyway, you should include its header.


    Do you understand what CADisplayLink is for? It's for refreshing the screen when some part of your content needs to be redrawn. If the phone is inactive or locked, then your content isn't showing, so logically, none of your content needs redrawing.

    Please describe exactly what you're trying to accomplish. If you're just doing a timer, then see the NSTimer class. CADisplayLink is only for timed screen redrawing.
     
  6. Poy77, Jun 12, 2011
    Last edited by a moderator: Jun 12, 2011

    Poy77 thread starter macrumors newbie

    Joined:
    Sep 5, 2010
    #6
    It is countdown counter. Second by second it shows a new value and in zero it will play an audio file.
    ---
    Code:
    // Timer event is called whenever the timer fires
    - (void)decreaseTimer:(id)sender
    {
        ...
        /* Update timer count */
        timerCount--;
    /* Calculate new digits */
            newSecondsHighDigit = (timerCount%60)/10;
            newSecondsLowDigit = (timerCount%60)%10;
            newMinutesDigit = timerCount/60;
        
            if(newSecondsLowDigit != oldSecondsLowDigit)
            {
                [displayLowDigit setNewDisplayNumber:newSecondsLowDigit];
            }
            if(newSecondsHighDigit != oldSecondsHighDigit)
            {
                [displayHighDigit setNewDisplayNumber:newSecondsHighDigit];
            }
            if(newMinutesDigit != oldMinutesDigit)
            {
                [displayMinuteDigit setNewDisplayNumber:newMinutesDigit];
            }
       ...
    }
    ---
    
    ---    
        displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(decreaseTimer:)];
        [displayLink setFrameInterval:60];
        [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    ---
    I have implemented custom numbers of 7-segment display. I use CGContextSetRGBFillColor function to draw a new number in 7-segment display. 7-segment display is own class. Would it be better to use NSTimer in my case?

    Display:
    http://dl.dropbox.com/u/25577/ObjectiveC/Screen shot 2011-06-12 at 22.26.33.png
    Warnings:
    http://dl.dropbox.com/u/25577/ObjectiveC/Screen shot 2011-06-12 at 22.16.15.png
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Yes.


    And you still haven't given any reason why you're using a CADisplayLink like this:
    Code:
    displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(decreaseTimer:)];
    
    instead of like this:
    Code:
    displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(decreaseTimer:)];
    
    It's a weird way to use a class, and it wasn't used that way in the original Apple example you started from, according to your original post.

    If you don't understand when to use NSClassFromString() or why, then you won't understand why you're getting the warnings. After you improve your understanding, the way to remove the warnings should become clear.
     

Share This Page