It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
我的第一个视图控制器中的NSDate不会刷新,直到我转到另一个视图控制器,然后再返回它。知道为什么会这样吗?

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];


方法:

-(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];
}


谢谢!

编辑:问题解决了,这要感谢下面的人提供了有用的答案。看起来其他没有给出答案的人都关闭了问题,但是下面给出了答案的两个人都工作了!

最佳答案

@HotLicks所说的fadeIn很好,但是您要在哪里更改新日期的UILabel?您需要这样做以显示更改的日期,对吗?

-(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];
    ....

 }


更新:这是希望为@reez工作的东西-哦,我现在知道了。将此类函数放在viewWillAppear中不是一个好主意,因为有时会缓存视图,并且不会调用此方法。最好将此方法放在viewDidLoad中。看看是否可行...

关于ios - NSDate不刷新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16660640/

10-09 09:52