Eigenschaft aus anderer Methode aufrufen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Eigenschaft aus anderer Methode aufrufen

    Hallo,

    ich will einen Timer erstellen, der auch weiterläuft wenn die App im Hintergrund ist.

    Soweit bekannt geht das nur über Umwege, in dem ich mir die aktuelle Zeit in eine Datei schreibe.

    Sobald die App wieder in den Vordergrund tritt, lese ich die Zeit wieder aus der Datei, ermittle die Differenzzeit zur aktuellen Zeit, und addiere diese auf meinen Timer drauf. Dann sieht es so aus als ob der Timer weitergelaufen wäre.

    Frage ist:
    Wie mache ich das am besten?


    Mein bissheriges Vorgehen:

    Die Timerwert steht in einem UILabel-Text, welcher in einer Klasse "myController" definiert ist und auf einem View dargestellt wird.

    Quellcode

    1. @interface myController : UIViewController
    2. {
    3. UILabel* myLabel;
    4. }
    5. @property (nonatomic, retain) IBOutlet UILabel* myLabel;




    In der Methode "applicationWillTerminate" in meinem ApplicationDelegate soll jetzt dieser UILabel-Text weggeschrieben werden.

    Quellcode

    1. - (void) applicationWillTerminate:(UIApplication *)application
    2. {
    3. fileIO=[[FileIO alloc] init];
    4. [fileIO writeTimerFile:myLabel.text];
    5. [fileIO release];
    6. }




    Das Problem:
    Diese Eigenschaft ist hier nicht bekannt, da in einer anderen Klasse definiert!


    Wie greife ich jetzt am besten auf myLabel.text zu?

    Der Zugriff auf eine Methode einer anderen Klasse (FileIO) wird hier ebenfalls durchgeführt und funktioniert auch.




    Gruß
    Chris
  • Hallo ramo,

    klasse. Das funktioniert jetzt!

    Aber 2 Dinge sind noch offen:


    1.) Es kommt ein Warning
    UIViewContontroller may not respont to myLabel

    2.)
    Im meiner readTimerMethode lese ich die Daten wieder ein.
    Ein NSLog zeigt an der Stelle das der String korrekt gelesen wurde.

    In applicationDidBecomeActive ist das Label aber null!


    Quellcode

    1. - (void) applicationDidBecomeActive:(UIApplication *)application
    2. {
    3. fileIO=[[FileIO alloc] init];
    4. [fileIO readTimerFile:[[self.myController myLabel]text]];
    5. NSLog(@"applicationDidBecomeActive %@",[[self.SpeedAltitudeController continuousLabel]text]);
    6. [fileIO release];
    7. }



    Quellcode

    1. - (void) readTimerFile: (NSString*) timerstring
    2. {
    3. NSString *documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    4. NSString *filePath =[documentsPath stringByAppendingPathComponent:@"timer.txt"];
    5. NSFileHandle *fileHandle =[NSFileHandle fileHandleForReadingAtPath:filePath];
    6. NSData *data =[fileHandle readDataToEndOfFile];
    7. NSString *configString =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    8. NSLog (@"lese timerstring aus Datei: %@",[ NSString stringWithString: configString]);
    9. timerstring=[ NSString stringWithString: configString];
    10. NSLog(@"read timerstring %@",timerstring);
    11. }
    Alles anzeigen



    Vielleicht hast du noch einen Tip für mich.
  • zu1 .)

    /Entwicklung/i/Classes/xAppDelegate.m: In function '-[AppDelegate applicationDidBecomeActive:]':
    /Entwicklung/i/Classes/xAppDelegate.m:144: warning: 'UIViewController' may not respond to '-continuousLabel'
    /Entwicklung/i/Classes/xAppDelegate.m:144: warning: (Messages without a matching method signature
    /Entwicklung/i/Classes/xAppDelegate.m:144: warning: will be assumed to return 'id' and accept
    /Entwicklung/i/Classes/xAppDelegate.m:144: warning: '...' as arguments.)
    /Entwicklung/i/Classes/xAppDelegate.m:146: warning: 'UIViewController' may not respond to '-continuousLabel'


    Ich nehme mal an du meinst die Setter/Getter nicht in der MEthode readTimerFile, sondern von "continuousLabel".
    Ja, die sind normal im Header der Klasse angelegt.

    Quellcode

    1. @interface SpeedAltitudeController : UIViewController <CLLocationManagerDelegate>
    2. {
    3. UILabel* continuousLabel;
    4. }
    5. @property (nonatomic, retain) IBOutlet UILabel* continuousLabel;
    6. @end
    7. bzw.
    8. @implementation SpeedAltitudeController
    9. @synthesize continuousLabel;
    10. .....
    11. @end
    Alles anzeigen