본문 바로가기
모바일개발(Mobile Dev)/IOS개발(ObjectC)

COUNTDOWN TIMER

by 테크한스 2015. 11. 19.
반응형

Explaining each steps to create your COUNTDOWN TIMER:

- Step 1:  Create a New Project. 1) Launch a Xcode and create a new project with a new Project with a singleView template which will work as entry point for project and give CoundownTimer2 as project name and class prefix as CoundownTimer2. 

Uncheck StoryBord if you want you you can use StoryBord also ,Uncheck UnitTest option and let ARC remain checked. 

You can use target device as iPhone and iPad any,but in this example i am using iPhone Save the project in your desired location. 




#import <UIKit/UIKit.h> 

@interface CoundownTimer2ViewController : UIViewController 

// label of clock      

IBOutlet UILabel *clockLabel; //this is date object for getting today date.      

NSDate *databaseDate; //timer object this actually used to call update label value after every seconds      

NSTimer *timer; //from this we are taking minutes from user     

UITextField *txtTimeInMinute;      

}

@property(nonatomic, retain) IBOutlet UILabel *clockLabel; 

@property(nonatomic, retain) NSDate *databaseDate; 

@end  





 Step 2: Design your application  using Xib   Add label on xib . in which you have to add timer. 

@implementation CoundownTimer2ViewController //this i am synthesizing value for label taking value inside it.  

@synthesize clockLabel; 

@synthesize databaseDate; 

- (void) showClock {     

NSTimeInterval remainingSec = [databaseDate timeIntervalSinceNow];     

if (!timer || remainingSec <= 0) {        

 [timer invalidate];         

timer = nil;         // getting time from text field or passing value init here i am passing 120 as a heard code value and multiplying with 60 to convert into seconds         

self.databaseDate = [NSDate dateWithTimeIntervalSinceNow:(120*60)];         

remainingSec = [databaseDate timeIntervalSinceNow]; //timer value calling timer continousley         

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

}      //here showing value in hours     


NSInteger hours = remainingSec / 3600;     

NSInteger remainder = ((NSInteger)remainingSec)% 3600; //here showing value in minute     

NSInteger minutes = remainder / 60; //here i am showing value in seconds     

NSInteger seconds = remainder % 60;     //here showing value for text label     

clockLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds]; 

- (void)viewDidLoad {     

[super viewDidLoad];     

[self showClock]; // Do any additional setup after loading the view, typically from a nib. 

- (void)didReceiveMemoryWarning {     

[super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. 


- See more at: http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=94c9a581-dcd4-43b9-ab9a-127ae54698f6#sthash.o3wG3GJD.dpuf



반응형