2012-02-13 16 views
5

¿Cómo puedo cambiar el código por lo que tiene HH: MM: SS (horas, minutos, segundos,Usando un NSTimer para HH: MM: SS?

Y usted me puede decir si tengo que añadir código en .h o .m por lo sabido cuál

en el momento en que sube como 1, 2, 3, 4, etc.

chicos

hi sólo para que usted conocido que soy cebo de un aficionado le copiar y pegar, así que conozco lo que quiere decir gracias

tipo Saludos

Paul

.h

@interface FirstViewController : UIViewController { 

    IBOutlet UILabel *time; 

    NSTimer *myticker; 

    //declare baseDate 
    NSDate* baseDate; 

} 

-(IBAction)stop; 
-(IBAction)reset; 

@end 

.m

#import "FirstViewController.h" 

@implementation FirstViewController 

-(IBAction)start { 
    [myticker invalidate]; 
    baseDate = [NSDate date]; 
    myticker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showActivity) userInfo:nil repeats:YES]; 
} 

-(IBAction)stop;{ 

    [myticker invalidate]; 
    myticker = nil; 
} 
-(IBAction)reset;{ 

    time.text = @"00:00:00"; 
} 
-(void)showActivity { 
    NSTimeInterval interval = [baseDate timeIntervalSinceNow]; 
    NSUInteger seconds = ABS((int)interval); 
    NSUInteger minutes = seconds/60; 
    NSUInteger hours = minutes/60; 
    time.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes%60, seconds%60]; 
} 
+0

Si desea medir la hora, le sugiero que use la clase NSDate en lugar de incrementar alguna variable entera arbitraria. – lawicko

+0

posible duplicado de [NSTimer - Cronómetro] (http://stackoverflow.com/questions/3180899/nstimer-stopwatch) – Caleb

+0

¿dónde pongo el NSdate? ¿Reemplace todo el NSTimer? –

Respuesta

7

En primer lugar, declarar fechabase variable en su FirstViewController.h, así:

@interface FirstViewController : UIViewController { 

    IBOutlet UILabel *time; 

    NSTimer *myticker; 

    //declare baseDate 
    NSDate* baseDate; 
} 

Luego, en el FirstViewControl ler.m método de inicio añadir baseDate = [NSDate date] así:

-(IBAction)start { 
    [myticker invalidate]; 
    baseDate = [NSDate date]; 
    myticker = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(showActivity) userInfo:nil repeats:YES]; 
} 

Después de eso, cambiar su showActivity método para tener este aspecto:

-(void)showActivity { 
    NSTimeInterval interval = [baseDate timeIntervalSinceNow]; 
    double intpart; 
    double fractional = modf(interval, &intpart); 
    NSUInteger hundredth = ABS((int)(fractional*100)); 
    NSUInteger seconds = ABS((int)interval); 
    NSUInteger minutes = seconds/60; 
    NSUInteger hours = minutes/60; 
    time.text = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", hours, minutes%60, seconds%60, hundredth]; 
} 

También tenga en cuenta, que va a tener que cambiar el intervalo valor para su temporizador; de lo contrario, su etiqueta solo se actualizará una vez por segundo.

+0

Hola, ¿podría agregarlo en mi código anterior porque estoy encontrando esto difícil de entender? declarar la variable basedate ???? gracias –

+0

¿Qué paso te resulta difícil de entender? – lawicko

+0

donde a los comicios pongo baseDate = [NSDate date]; en mi código y - (void) showActivity { NSTimeInterval interval = [baseDate timeIntervalSinceNow]; NSUEntrante segundos = ABS (intervalo (int)); NSUEntero minutos = segundos/60; NSUEntero horas = minutos/60; time.text = [NSString stringWithFormat: @ "% 02d:% 02d:% 02d", horas, minutos% 60, segundos% 60]; } –

Cuestiones relacionadas