Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Countdown timer from user input?

VoodooBabaVoodooBaba Posts: 12Registered Users
Hi, I'm new in here. Just to the point, I want to make countdown timer, time is setting by user just say that is 00:10:00 (hh:mm:ss). I already made it the code for inputting using text label but it show at label but I don't know how to write code from taking my input time that will use for my countdown timer. It's been a week I search over Internet and from book manual but I still didn't get the answer. I ask people but they suggest me to read manual about NSDate, timer or whatever at apple documents. My problem is English is not my native language so I can't really understand English but using google translation gives me different meaning. I also can't ask people near me at my place because not all of them understand Xcode. I lack information, friend and manual. I downloaded all PDF book about Xcode but it can't give me what I need. Just like this problem, it already for a week to get answer but it didn't. So this is my first time for me to ask in this forum. I'm not meaning asking you to help me write down all the code from scratch, but I would be thank you for helping me to provide the code.

Thank you for watching my thread.
Post edited by VoodooBaba on

Replies

  • Duncan CDuncan C Posts: 8,032Tutorial Authors, Registered Users
    VoodooBaba;438889 said:
    Hi, I'm new in here. Just to the point, I want to make countdown timer, time is setting by user just say that is 00:10:00 (hh:mm:ss). I already made it the code for inputting using text label but it show at label but I don't know how to write code from taking my input time that will use for my countdown timer. It's been a week I search over Internet and from book manual but I still didn't get the answer. I ask people but they suggest me to read manual about NSDate, timer or whatever at apple documents. My problem is English is not my native language so I can't really understand English but using google translation gives me different meaning. I also can't ask people near me at my place because not all of them understand Xcode. I lack information, friend and manual. I downloaded all PDF book about Xcode but it can't give me what I need. Just like this problem, it already for a week to get answer but it didn't. So this is my first time for me to ask in this forum. I'm not meaning asking you to help me write down all the code from scratch, but I would be thank you for helping me to provide the code.

    Thank you for watching my thread.

    There are 2 parts to what you want to do: Getting a time value from the user, and then running the count-down timer.


    Part 1: Collect a time from the user and interpret it. There are a number of different ways to do this. You could set up a UIPickerView with wheels for hours, minutes, and seconds, then get the values for each. You could prompt the user to enter a string with hours, minutes and seconds separated with colons, and then use a date formatter or other string parsing code to break it up.

    If you are just starting out, it might be easier to set up a separate UITextField for the hours value, the minutes value, and the seconds value.

    Part 2: start a timer to display the time as it counts down
    Once you have values for the desired hours/minutes/seconds from the user, you need to run a timer.

    Create a couple of instance variables of type NSTimeInterval. Let's call them delay and finishTime.

    Also create an instance variable "secondsTimer" of type NSTimer*

    First calculate the total number of seconds (hours * 3600 + minutes * 60 + seconds). Put the results in your delay instance variable.

    When the user tap your start button, do the following:

    Calculate the future time when the timer will be finished:

    finishTime = 
    [NSDate timeIntervalSinceReferenceDate] + delay;


    Then start a timer that runs every second:

    secondsTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 
    target: self
    selector: @selector(updateTime:)
    userInfo: nil
    repeats: YES];


    And write an updateTime method:

    - (void) updateTime: (NSTimer *) timer
    {
    int scratch;
    int hours, minutes, seconds; //for dislay
    int remainingTime =
    finishTime - [NSDate timeIntervalSinceReferenceDate];
    if (remainingTime <=0)
    {
    [secondsTimer invalidate];
    secondsTimer = nil;
    remainingTime = 0;
    }
    hours = remainingTime / 3600;
    scratch = remainingTime % 3600;
    minutes = scratch / 60;
    seconds = scratch % 60;
    //Now display remaining hours, minutes, and seconds to labels in your
    //interface.
    }
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • VoodooBabaVoodooBaba Posts: 12Registered Users
    Thank you Duncan, i already change for input using UIPickerView that will pick data and show at labelTime. but i still don't understand how NSDate get information from labelTime for each variable that i used it is hour, minute and second? and again, i want the process of countdown is start after button START is touch. so it means that i must write code inside
    - (IBAction)startBtn:(id)sender
    , right? so what is the first step for me to write?

    my pickerview is like this :

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
    int hour = [picker selectedRowInComponent:0];
    int minute = [picker selectedRowInComponent:1];
    int second = [picker selectedRowInComponent:2];
    NSString *time = @\"\";
    labelTime.text = [time stringByAppendingFormat:@\"%d : %d : %d\", hour , minute, second];
    }


    as i asked before, i don't know how to take data from my labelTime to use at countdown timer. please teach me.
  • VoodooBabaVoodooBaba Posts: 12Registered Users
    I feel hopeless now. Is there any sample countdown project for me to learn?
  • Duncan CDuncan C Posts: 8,032Tutorial Authors, Registered Users
    VoodooBaba;438956 said:
    Thank you Duncan, i already change for input using UIPickerView that will pick data and show at labelTime. but i still don't understand how NSDate get information from labelTime for each variable that i used it is hour, minute and second? and again, i want the process of countdown is start after button START is touch. so it means that i must write code inside
    - (IBAction)startBtn:(id)sender
    , right? so what is the first step for me to write?

    my pickerview is like this :

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
    int hour = [picker selectedRowInComponent:0];
    int minute = [picker selectedRowInComponent:1];
    int second = [picker selectedRowInComponent:2];
    NSString *time = @\"\";
    labelTime.text = [time stringByAppendingFormat:@\"%d : %d : %d\", hour , minute, second];
    }


    as i asked before, i don't know how to take data from my labelTime to use at countdown timer. please teach me.

    Don't try to get the values from a label. Bad idea. View objects are for displaying information, not saving it.

    Instead of declaring local variables hour, minute, and second, make those 3 variables instance variables. They will then be available in any instance method in that class. If you make them public properties, they will be available ANYWHERE in your program that has access to the object holding the values.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • VoodooBabaVoodooBaba Posts: 12Registered Users
    Thx Duncan but i still don't understand. Im just beginner, so i really new with this. Just need your help, please give me straigh code, because my brain is blank.
    just like i said, my time input for countdown timer is from

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
    int hour = [picker selectedRowInComponent:0];
    int minute = [picker selectedRowInComponent:1];
    int second = [picker selectedRowInComponent:2];
    NSString *time = @\"\";
    labelTime.text = [time stringByAppendingFormat:@\"%d : %d : %d\", hour , minute, second];
    }


    but your code seems countdown from update time not from user input.

    i just want to put countdown code into my button below
    - (IBAction)startBtn:(id)sender {

    if the button is press then countdown is starting.
  • Duncan CDuncan C Posts: 8,032Tutorial Authors, Registered Users
    VoodooBaba;439156 said:
    Thx Duncan but i still don't understand. Im just beginner, so i really new with this. Just need your help, please give me straigh code, because my brain is blank.
    just like i said, my time input for countdown timer is from

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
    int hour = [picker selectedRowInComponent:0];
    int minute = [picker selectedRowInComponent:1];
    int second = [picker selectedRowInComponent:2];
    NSString *time = @\"\";
    labelTime.text = [time stringByAppendingFormat:@\"%d : %d : %d\", hour , minute, second];
    }


    but your code seems countdown from update time not from user input.

    i just want to put countdown code into my button below
    - (IBAction)startBtn:(id)sender {

    if the button is press then countdown is starting.

    Move the declarations of hour, minute, and second into your .h file to make them instance variables. Also add a timer instance variable an ints totalSeconds, and a time interval endTime.

    If your class is SomeClass, you'd add the variables like this: (edit it to fit your program)

    @interface SomeClass
    {
    int hour;
    int minute;
    int second;
    int totalSeconds;
    NSTimeInterval endTime;
    NSTimer *countdownTimer;
    //other instance vars go here
    }


    Then, in your pickerView:didSelectRow: method, change the code to save the hour, minute, and second values to the instance variables instead of to local variables:

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
    //Save hours, minutes and seconds to instance variables.
    hour = [picker selectedRowInComponent:0];
    minute = [picker selectedRowInComponent:1];
    second = [picker selectedRowInComponent:2];

    //Also calculate the total seconds
    totalSeconds = hour * 3600 + minute * 60 + second;
    NSString *time = @\"\";
    labelTime.text = [time stringByAppendingFormat:@\"%d : %d : %d\", hour , minute, second];
    }



    Then in your button press action:

    - (IBAction)startBtn:(id)sender 
    {
    //Figure out the end time as a number of seconds since Jan 1, 2001
    endTime = [NSDate timeIntervalSinceReferenceDate] + totalSeconds;

    //Start a timer
    countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0
    target: self
    selector: @selector(updateLabel:)
    userInfo: nil
    repeats: YES];
    }

    //The method that the timer calls once a second:
    - (void) updateLabel: (NSTimer *) timer;
    {
    //Figure out how many seconds remain to our end time.
    NSTimeInterval remainingTime = endTime - [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval scratch;
    if (remainingTime <= 0)
    {
    //If the end time has passed, stop the timer.
    [timer invalidate];
    //Force remainingTime to zero in case we missed the exact instant and it went negative.
    remainingTime = 0;
    }

    //Figure out how many hours, minutes, and seconds remain.
    int remainingHours remainingTime / 3600;
    scratch = remainingTime % 3600;
    int remainingMinutes = scratch / 60;
    int remainingSeconds = scratch % 60;
    //Display hours, minutes, and seconds to your labels, and do whatever else you need to do...
    }
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • VoodooBabaVoodooBaba Posts: 12Registered Users
    You save me Duncan. Thank you for your help and this countdown lesson. It works now. GBU Duncan.
  • VoodooBabaVoodooBaba Posts: 12Registered Users
    Duncan. I have stupid question, please forgive me. Why my counter always less 2 seconds to start? Just example, when i give it 60 second, it start counting down from 58? is it normal? And sometimes it has delay for 1 or 2 second in the middle counting down my timer. thank you.
  • Duncan CDuncan C Posts: 8,032Tutorial Authors, Registered Users
    VoodooBaba;439338 said:
    Duncan. I have stupid question, please forgive me. Why my counter always less 2 seconds to start? Just example, when i give it 60 second, it start counting down from 58? is it normal? And sometimes it has delay for 1 or 2 second in the middle counting down my timer. thank you.
    I would expect 1 second. Two seconds is a bit odd.

    First, you should call updateLabel before beginning the timer so you display the full time interval before the first second passes.

    Second, I guess the elapsed time calculation is a tiny fraction of a second passed the specified amount, and truncates down rather than rounding. Try adding like .05 seconds to the totalSeconds value you calculate from the picker. (You might need to make it a double instead of an int - I don't remember what type I used).
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • VoodooBabaVoodooBaba Posts: 12Registered Users
    I actually writing code just say i have 2 timeInterval, that is remainingTime1 and remainingTime2. I want to write, if remainingTime1 countdown to 0 then start to do remainingTime2 and if remainingTime2 is finish counting down to 0 then back to to remainingTime1, if remainingTime1 countdown to 0 then start to do remainingTime2 and if remainingTime2 is finish counting down to 0 then back to to remainingTime1 and so on until my loop finish. Just case remainingTime1 and remainingTime2 has fix time, just say 1 minute for counting down at remainingTime1 and 30 seconds for counting down at remainingTime2. Seems like loop, just say loop for 5 times. How to write it?

    - (void) updateLabel: (NSTimer *) timer;
    {
    NSTimeInterval remainingTime = endTime1 - [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval scratch;
    if (remainingTime <= 0) {
    [timer invalidate];
    remainingTime1 = 0;
    }
    hour = remainingTime / 3600;
    scratch1 = remainingTime % 3600;
    minute = scratch / 60;
    second = scratch % 60;
    displayCountDown.text = [NSString stringWithFormat:@\"%d : %d : %d\", hour , minute, second];
    }


    This button is to trigger to start counting down my clock

    - (IBAction)startBtn:(id)sender {
    endTime1 = [NSDate timeIntervalSinceReferenceDate] + totalSecondTime;
    endTime2 = [NSDate timeIntervalSinceReferenceDate] + totalSecondRest;
    countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(updateLabel:) userInfo: nil repeats: YES];
    }


    optional, just for information

    totalSecondTime = hour * 3600 + minute * 60 + second;
    totalSecondRest = hour * 3600 + minute * 60 + second;


    I don't know how to loop 5 times. I don't have example case like mine here, not also in internet. I search it everywhere but i couldn't find it. Please.

    ________________
    1 Corinthians 3:2
    I have fed you with amilk, and not with meat: for hitherto ye were not able to bear it, neither yet now are ye able.

    This really like me :)
  • Duncan CDuncan C Posts: 8,032Tutorial Authors, Registered Users
    VoodooBaba;439369 said:
    I actually writing code just say i have 2 timeInterval, that is remainingTime1 and remainingTime2. I want to write, if remainingTime1 countdown to 0 then start to do remainingTime2 and if remainingTime2 is finish counting down to 0 then back to to remainingTime1, if remainingTime1 countdown to 0 then start to do remainingTime2 and if remainingTime2 is finish counting down to 0 then back to to remainingTime1 and so on until my loop finish. Just case remainingTime1 and remainingTime2 has fix time, just say 1 minute for counting down at remainingTime1 and 30 seconds for counting down at remainingTime2. Seems like loop, just say loop for 5 times. How to write it?

    - (void) updateLabel: (NSTimer *) timer;
    {
    NSTimeInterval remainingTime = endTime1 - [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval scratch;
    if (remainingTime <= 0) {
    [timer invalidate];
    remainingTime1 = 0;
    }
    hour = remainingTime / 3600;
    scratch1 = remainingTime % 3600;
    minute = scratch / 60;
    second = scratch % 60;
    displayCountDown.text = [NSString stringWithFormat:@\"%d : %d : %d\", hour , minute, second];
    }


    This button is to trigger to start counting down my clock

    - (IBAction)startBtn:(id)sender {
    endTime1 = [NSDate timeIntervalSinceReferenceDate] + totalSecondTime;
    endTime2 = [NSDate timeIntervalSinceReferenceDate] + totalSecondRest;
    countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(updateLabel:) userInfo: nil repeats: YES];
    }


    optional, just for information

    totalSecondTime = hour * 3600 + minute * 60 + second;
    totalSecondRest = hour * 3600 + minute * 60 + second;


    I don't know how to loop 5 times. I don't have example case like mine here, not also in internet. I search it everywhere but i couldn't find it. Please.

    ________________
    1 Corinthians 3:2
    I have fed you with amilk, and not with meat: for hitherto ye were not able to bear it, neither yet now are ye able.

    This really like me :)

    By figuring out what you want to do, figuring out the logical sequence of actions that achieves that goal, and writing code that implements that behavior. I'm not doing any more of your work for you. Sorry.

    I already did WAYYYY too much of your work for you.

    If you write it yourself and have bugs you can't figure out, we can help, but programming is a creative process. You need to figure out what you want to do and how to do it.

    If you'd like to hire my company to develop your app for you, that's a different story...
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • VoodooBabaVoodooBaba Posts: 12Registered Users
    okay sorry, it only for my college project. thanks for help
  • Duncan CDuncan C Posts: 8,032Tutorial Authors, Registered Users
    VoodooBaba;439376 said:
    okay sorry, it only for my college project. thanks for help
    Great, so I did a big part of your college project for you? That's cheating, in my book.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • VoodooBabaVoodooBaba Posts: 12Registered Users
    im sorry, i dont mean be like that. i make this apps only for my college project for counting how many people will enter a room with time interval. we cant do it with manual stopwatch because we cant set time with schedule. and i think iphone is more flexible than using computer. we can make software using computer to execute but we think about "flexible" situattion, that is, pull it up from your pocket and touch, set the time and run it, no need pc, electricity, battery or boot time.
    my project is counting people to entering exhibition or even concert who has lot people enter with safety.

    i hope you understand what i mean. theres nothing to do with apps that i made now.

    ps. thx for your help and be positive thinking please. i need help doesnt mean i didnt make it from scratch. my app and my project is separate things. no app like i need sell at apple apps. if someone wont teach me, perhaps theres still good people to make for me countdown timerr like i need. thank you and sayonara.
    sup
  • aslam26aslam26 Posts: 2New Users
    Thnx Duncan C, above code was very helpful to understand NSTimer. Can plz help me in pausing countDownTimer and resuming it back from previous pause time. Thnx in advance.
  • Duncan CDuncan C Posts: 8,032Tutorial Authors, Registered Users
    aslam26 said:

    Thnx Duncan C, above code was very helpful to understand NSTimer. Can plz help me in pausing countDownTimer and resuming it back from previous pause time. Thnx in advance.

    You can't really pause a timer.

    When you want to pause your timer you need to

    Calculate the amount of time remaining and save that to an instance variable
    Invalidate your current timer and set it to nil.

    Then when the user asks to continue the timer you start a new timer using your saved remaining time iVar.


    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • aslam26aslam26 Posts: 2New Users
    Thnx Duncan. i agree with you that timer can't be paused i tired all the stuff but was unsuccessful
    , only thing is it should be invalidate. But how to calculate remaining amount of time and save it, and again fire timer with saved remaining time. Can you plz post the code, i m struck in this last feature pausing and resuming timer.
    Lastly one quick question, "as you timer can't be paused", should timer be running when app goes background(home button pressed) does Apple allows it?

    Regards
  • Duncan CDuncan C Posts: 8,032Tutorial Authors, Registered Users
    aslam26 said:

    Thnx Duncan. i agree with you that timer can't be paused i tired all the stuff but was unsuccessful
    , only thing is it should be invalidate. But how to calculate remaining amount of time and save it, and again fire timer with saved remaining time. Can you plz post the code, i m struck in this last feature pausing and resuming timer.
    Lastly one quick question, "as you timer can't be paused", should timer be running when app goes background(home button pressed) does Apple allows it?

    Regards

    No, you need to think this through for yourself. I've already given you more code than I should.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.