zoukankan      html  css  js  c++  java
  • [IOS]iphone之在视图上显示当前的时间,并且时间还在走。

    iphone之在视图上显示当前的时间,并且时间还在走。

    在RootViewController.h中:

    #import <UIKit/UIKit.h>

    @interface RootViewController : UIViewController {

    NSTimer *_timer;

    UILabel *timeLabel;

    }

    @property (nonatomic,retain) UILabel *timeLabel;

    @end

    在RootViewController.m中:

    #import "RootViewController.h"

    #import <stdarg.h>

    @implementation RootViewController

    @synthesize timeLabel;

    -(id)init

    {

    self = [super init];

    if (self) {

    }

    return self;

    }

    - (void)loadView {

    UIView *back = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];

    back.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

    self.view = back;

    [back release];

    }

    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

    - (void)viewDidLoad {

    [super viewDidLoad];

    //

    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerFunc) userInfo:nil repeats:YES];



    //显示时间

    timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 130, 200, 30)];

    timeLabel.backgroundColor = [UIColor clearColor];

    [self.view addSubview:timeLabel];

    }



    //每一秒都被调用一次

    - (void)timerFunc

    {

    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease];

    [formatter setDateFormat:@"MM/dd/YY HH:mm:ss"];

    NSString *timestamp = [formatter stringFromDate:[NSDate date]];

    [timeLabel setText:timestamp];//时间在变化的语句

    NSLog(@"%@",timestamp);

    }


    此代码用了计数器的原理,

    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerFunc) userInfo:nil repeats:YES];
    每隔1秒调用一下timerFunc方法。
    [好好学习,天天向上]

  • 相关阅读:
    1-7周成绩总结
    1-6周成绩总结
    第七周学习笔记
    前五周测验成绩总结
    第六周学习笔记
    第五周学习笔记
    2018-2019-2 20189206 Python3学习
    2018-2019-2 20189206 安全工具的学习
    2018-2019-2 20189206 《网络攻防实践》 第一周作业
    2018-2019-1 20189206 《Linux内核原理与分析》第九周作业
  • 原文地址:https://www.cnblogs.com/iphone520/p/2225103.html
Copyright © 2011-2022 走看看