Geburtstags Timer

Melissa2

Neues Mitglied
Thread Starter
Dabei seit
27.05.2013
Beiträge
7
Reaktionspunkte
0
Hallo leute
bin neu bei euch... :)

ich arbeite schon länger an einen kleinen Projekt
ich möchte einen Geburtstags Kalender erstellen!

Den Timer hab ich schon geschafft, das Problem ist nur das er nicht bei 0 stehen bleibt sondern ins - läuft...

kann mir jemand bitte helfen :)

Danke Danke

Code:
 [COLOR=#D200A8][FONT=Menlo]@implementation[/FONT][/COLOR][FONT=Menlo] ViewController[/FONT][FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]-([COLOR=#d200a8]void[/COLOR])updateLabel {[/FONT]
[FONT=Menlo]    [/FONT]
[COLOR=#7E14B1][FONT=Menlo][COLOR=#000000]    [/COLOR]NSDateFormatter[COLOR=#000000] *dateformatter = [[[/COLOR]NSDateFormatter[COLOR=#000000] [/COLOR][COLOR=#480088]alloc[/COLOR][COLOR=#000000]] [/COLOR][COLOR=#480088]init[/COLOR][COLOR=#000000]];[/COLOR][/FONT][/COLOR]
[FONT=Menlo]    [dateformatter [COLOR=#480088]setDateFormat[/COLOR]:[COLOR=#ea0000]@"dd/MM/yyyy HH:mm:SS"[/COLOR]];[/FONT]
[FONT=Menlo]    [/FONT]
[COLOR=#EA0000][FONT=Menlo][COLOR=#000000]    [/COLOR][COLOR=#7e14b1]NSString[/COLOR][COLOR=#000000] *fathersDay = [/COLOR]@"27/05/2013 18:03:13"[COLOR=#000000];[/COLOR][/FONT][/COLOR]
[FONT=Menlo]    [COLOR=#7e14b1]NSDate[/COLOR] *currentDate = [dateformatter [COLOR=#480088]dateFromString[/COLOR]:fathersDay];[/FONT]
[FONT=Menlo]    [/FONT]
[COLOR=#7E14B1][FONT=Menlo][COLOR=#000000]    [/COLOR]NSCalendar[COLOR=#000000] *gregorian = [[[/COLOR]NSCalendar[COLOR=#000000] [/COLOR][COLOR=#480088]alloc[/COLOR][COLOR=#000000]] [/COLOR][COLOR=#480088]initWithCalendarIdentifier[/COLOR][COLOR=#000000]:[/COLOR]NSGregorianCalendar[COLOR=#000000]];[/COLOR][/FONT][/COLOR]
[COLOR=#480088][FONT=Menlo][COLOR=#000000]    [/COLOR][COLOR=#d200a8]int[/COLOR][COLOR=#000000] unitFlags = [/COLOR]NSDayCalendarUnit[COLOR=#000000] | [/COLOR]NSHourCalendarUnit[COLOR=#000000] | [/COLOR]NSMinuteCalendarUnit[COLOR=#000000] | [/COLOR]NSSecondCalendarUnit[COLOR=#000000];[/COLOR][/FONT][/COLOR]
[FONT=Menlo]    [COLOR=#7e14b1]NSDateComponents[/COLOR] *components = [gregorian [COLOR=#480088]components[/COLOR]:unitFlags [COLOR=#480088]fromDate[/COLOR]:[COLOR=#358289]today[/COLOR] [COLOR=#480088]toDate[/COLOR]:currentDate [COLOR=#480088]options[/COLOR]:[COLOR=#3a00e3]0[/COLOR]];[/FONT]
[FONT=Menlo]    [/FONT]
[FONT=Menlo]    [COLOR=#358289]countdownLabel[/COLOR].[COLOR=#7e14b1]text[/COLOR] = [[COLOR=#7e14b1]NSString[/COLOR] [COLOR=#480088]stringWithFormat[/COLOR]:[COLOR=#ea0000]@"%02dDays, %02dHour, %02dmin., %02dsec."[/COLOR], components.[COLOR=#480088]day[/COLOR], components.[COLOR=#480088]hour[/COLOR], components.[COLOR=#480088]minute[/COLOR], components.[COLOR=#480088]second[/COLOR] ];[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]    [/FONT]
[FONT=Menlo]    [/FONT]
[FONT=Menlo]    }[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]- ([COLOR=#d200a8]void[/COLOR])viewDidLoad {[/FONT]
[FONT=Menlo]    [COLOR=#d200a8]self[/COLOR]->[COLOR=#358289]today[/COLOR] = [[COLOR=#7e14b1]NSDate[/COLOR] [COLOR=#480088]date[/COLOR]];[/FONT]
[COLOR=#480088][FONT=Menlo][COLOR=#000000]    [/COLOR][COLOR=#d200a8]self[/COLOR][COLOR=#000000]->[/COLOR][COLOR=#358289]timer[/COLOR][COLOR=#000000] = [[/COLOR][COLOR=#7e14b1]NSTimer[/COLOR][COLOR=#000000] [/COLOR]scheduledTimerWithTimeInterval[COLOR=#000000]:([/COLOR][COLOR=#3a00e3]1.0[/COLOR][COLOR=#000000]) [/COLOR]target[COLOR=#000000]:[/COLOR][COLOR=#d200a8]self[/COLOR][COLOR=#000000] [/COLOR]selector[COLOR=#000000]:[/COLOR][COLOR=#d200a8]@selector[/COLOR][COLOR=#000000](updateLabel) [/COLOR]userInfo[COLOR=#000000]:[/COLOR][COLOR=#d200a8]nil[/COLOR][COLOR=#000000] [/COLOR]repeats[COLOR=#000000]:[/COLOR][COLOR=#d200a8]YES[/COLOR][COLOR=#000000]];[/COLOR][/FONT][/COLOR]
[COLOR=#480088][FONT=Menlo][COLOR=#000000]    [[/COLOR][COLOR=#d200a8]super[/COLOR][COLOR=#000000] [/COLOR]viewDidLoad[COLOR=#000000]];[/COLOR][/FONT][/COLOR]
[FONT=Menlo]    [/FONT]
[FONT=Menlo]}[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
[FONT=Menlo]
[/FONT]
 
Ich bin zwar kein Obj-C Programmierer ..., aber ich kann keine Abbruchsbedingung in deinem Code finden, du solltest also vergleichen, ob der aktuelle Zeitstempel == Zielzeitstempel ist (oder die Differenz == 0) und dann aussteigen. Schön wäre auch, wenn du die "repeats" Eigenschaft des Timers dann auf FALSE setzen würdest. Aber wie gesagt: ich kenn mich mit Obj-C nicht aus und kann darum keine konkrete Implementierungshilfe geben.
Viel Glück!
ruerueka
 
Code:
-(void)updateLabel:(NSTimer*)updateTimer
 {

NSDateFormatter *dateformatter = [[NSDateFormatteralloc] init];
    [dateformatter setDateFormat:@"dd/MM/yyyy HH:mm:SS"];

NSString *fathersDay = @"27/05/2013 18:03:13";
 NSDate *currentDate = [dateformatter dateFromString:fathersDay];

NSCalendar *gregorian = [[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar];
int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
 NSDateComponents *components = [gregorian components:unitFlags fromDate:today toDate:currentDate options:0];

 countdownLabel.text = [NSString stringWithFormat:@"%02dDays, %02dHour, %02dmin., %02dsec.", components.day, components.hour, components.minute, components.second ];

[updateTimer invalidate];


    }


- (void)viewDidLoad {
 self->today = [NSDate date];
self->timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];
    [superviewDidLoad];

}
 
Hallo,

Code:
[updateTimer invalidate];
Das löst ihr Problem aber nicht, da die Methode so genau nur einmal nach einer Sekunde aufgerufen wird.

Die korrekte Lösung ist aus dem Text von ruerueka zu entnehmen.

Viele Grüße

PS: Ein Timer ist wahrscheinlich eh nicht passend. Aber für den Anfang sollte das okay sein.
 
Hallo,


Das löst ihr Problem aber nicht, da die Methode so genau nur einmal nach einer Sekunde aufgerufen wird.

Die korrekte Lösung ist aus dem Text von ruerueka zu entnehmen.

Viele Grüße

PS: Ein Timer ist wahrscheinlich eh nicht passend. Aber für den Anfang sollte das okay sein.


Stimmt da fehlt die abfrage für das Enddatum.
 
Hallo Leute Danke Danke für die Tips
leider steh ich grad voll auf der Leitung!

Den Zeitstempel hab ich ja da:

der Timer läuft ja (im Hintergrund)

code:

NSDateComponents *components = [gregorian components:unitFlags fromDate:today toDate:currentDate options:0];

countdownLabel.text = [NSString stringWithFormat:mad:"%02dDays, %02dHour, %02dmin., %02dsec.", components.day, components.hour, components.minute, components.second ];


Danke für die infos! :)
Melissa
 
Mal ein Beispiel, sieh unten.

Viele Grüße

Code:
-(void)cancelTimer:(id)sender
{
    
    // …
    
    NSTimer *timer = self.repeatTimer;
    
    if(timer == nil)
    {
    
        return;
    
    }
    
    // …
    
    [timer invalidate];
    
    // …
    
    self.repeatTimer = nil;
    
    // …

}

-(void)startTimer:(id)sender
{
    
    // …
    
    [self cancelTimer:self];

    // …
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                      target:self
                                                    selector:@selector(firedTimer:)
                                                    userInfo:nil
                                                     repeats:NO];
    
    if(timer == nil)
    {
    
        return;
    
    }
    
    // …
    
    NSRunLoop *runLoop = [NSRunLoop mainRunLoop];
    
    if(runLoop == nil)
    {
    
        return;
    
    }
    
    // …
    
    self.repeatTimer = timer;
    
    // …
    
    [runLoop addTimer:timer
              forMode:NSRunLoopCommonModes];
    
    // …

}

-(void)firedTimer:(NSTimer*)timer
{
    
    // …
    
    [self cancelTimer:self];

    // …
    
    NSDate *compareDate = self.birtdayDate;
    
    if(compareDate == nil)
    {
    
        return;
    
    }
    
    // …
    
    NSDate *nowDate = [NSDate date];
    
    if(nowDate == nil)
    {
        
        return;
        
    }
    
    // …
    
    if([compareDate compare:nowDate] == NSOrderedDescending)
    {
        
        // …
    
        [self startTimer:self];
        
        // …

        return;
        
        // …
    
    }
    
    // …
    
    NSLog(@"Blubb…");
    
    // …

}
 
Hey cool danke :)
aber da weiss ja der timer nicht wann er aufhören soll oder zumindest ne zeitangabe :)
oder versteh ich das falsch ??? :-(
 
Hallo leute

if (time == 00)
{
[timer invalidate];
timer = nil;
}

aber irgendwas past nicht...

hilfeeee :-(
 
Die ersten Schritte zum Programmiern sind echt schwer, da es so viel Stoff auf einmal ist. Schau mal, ob du in der Bibliothek ein Einführungsbuch in Obj C z.B von Amin Negm-Awad bekommst. Oder "Mac-Programmierung für Kids" von Holger Hinzberg - das soll auch sehr gut sein. Amins Bücher hab ich selbst mal "angelesen", bin aber mangels Zeit nie produktiv geworden. Und dann gabs da eine Zeit lang noch den Video Blog Xcode von 0 auf 100, vielleicht hat jemand ne aktuelle URL zu einem vergleichbaren Projekt.
 
hmmm ja sorry hilft mir auch nicht weiter...
und wie stoppt der timer jetzt??
:)

melissa
 
little_pixel hat es doch schon geschrieben! Schaust Du auch mal in die Doku? Hast Du Dir den Code, den du bis jetzt hast, irgendwoher kopiert oder selbst geschrieben?
 
hallo so geschafft der Timer ist gestoppt
aber mit
" if ([today timeIntervalSinceDate:currentDate] <= 0 ) "

wie gehe ich jetzt am besten vor wenn der 2 timer jetzt anfangen soll?

bitte um info
Daaaankeee :)
 
Hallo,

ich denke, dass Du einen Logikfehler hast ;)

Du schreibst natürlich nicht für jede Person einen Timer.
Oder glaubst Du, dass Apple jeden neuen Tab in Safari wieder neu programmiert hat? ;)

Jede Person wird durch eine Instanz z.B. der Klasse "Person" repräsentiert.
Die Anzahl dieser Instanzen ist variabel und sollte mittels Controller verwaltet werden.

Ich kann Dich nur nochmals darauf hinweisen, auch wenn es so etwas von abgedroschen ist: Grundlagen.

Viele Grüße
 
  • Gefällt mir
Reaktionen: Christian1313
Zurück
Oben Unten