0

The NSDate in my first view Controller wont refresh until I go to another view controller and then come back to it. Any idea why that would be?

viewDidLoad:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fadein) userInfo:nil repeats:NO];
    [timer fire];

viewWillAppear:

// Date
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    NSString *dateToday = [formatter stringFromDate:[NSDate date]];
    [dateLabel setText:dateToday];
    dateLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];

Method:

-(void) fadein
{
    dateLabel.alpha = 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelay:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

    [UIView setAnimationDelegate:self];

    [UIView setAnimationDuration:1.0];
    dateLabel.alpha = 1;

    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
}

Thanks!

EDIT: Problem solved, thanks to the people below with helpful answers. Looks like other people that didn't give answers closed the question, but both people that gave answers below worked!

4

2 回答 2

1

What @HotLicks is saying is fadeIn is fine but where are you changing the UILabel with the new date? You need to do that to show the changed date right?

-(void) fadein
 {
    //just an example. change this to the format in which you are showing your date.
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY"];
    uilabelDate.text = [dateFormat stringFromDate:date];
    ....

 }

UPDATE: This is what hopefully worked for @reez - Oh I have got it now. Its not a good Idea to put such functions in viewWillAppear as sometimes the view would be cached and this method would not be called. Its better to put this method in viewDidLoad. See if that works...

于 2013-05-21T02:19:46.953 回答
1

The dateLabel text should change upon opening the app if it is a different day. You know what I mean? So on Sunday I opened the app, but Monday when I opened the app it still had the Sunday date as the dateLabel

From your comment, it looks like you're not refreshing the date when re-opening the app. viewWillAppear is not called if you press home button and reopen the app (I hope you're aware that this doesn't close/kill the app). You need to add a call to refreshDate from AppDelegate's applicationWillEnterForeground: method.

于 2013-05-21T07:34:18.607 回答