Implementation of the iOS digital clock

Source: Internet
Author: User

implementation of the iOS digital clock





The creation of the digital clock, we first prepare some parameters: The code is as follows:

#import "AppDelegate.h" @implementation appdelegate{    UILabel * _yearlabel;    UILabel * _monthlabel;    UILabel * _daylabel;    UILabel * _hourlabel;    UILabel * _minutelabel;    UILabel * _secondlabel;    Nsmutablearray * _datearray;    Nstimer * _timer;}

Then, we create the digital clock display interface: The code is as follows

1. Create an array to hold, year, month, day, time, minute, second

-(void) preparedataarray{    _datearray = [[Nsmutablearray alloc]initwithobjects:@ "Year", @ "month", @ "Day", @ "time", @ "Min", @ "seconds", NIL];}

2, we create the interface, the code is as follows:

-(void) prepareui{for (int i = 0; i<2; i++) {for (int j = 0; j<3; j + +) {UILabel * label = [U                        Ilabel Alloc]initwithframe:cgrectmake (70+100*j, 50+i*70, 50, 50)];            Label.backgroundcolor = [Uicolor Cyancolor];            Label.textalignment = Nstextalignmentcenter;            Label.text = [_datearray objectatindex:i*3+j];            Label.font = [Uifont fontwithname:@ "LCDD" size:35];            [Self.window Addsubview:label];        [Label release];    }}//Set year _yearlabel = [[UILabel alloc]initwithframe:cgrectmake (0, 50, 70, 50)];    _yearlabel.text = @ "0000";    _yearlabel.textcolor = [Uicolor Redcolor];    _yearlabel.textalignment = Nstextalignmentcenter;    _yearlabel.font = [Uifont fontwithname:@ "LCDD" size:30];    _yearlabel.backgroundcolor = [Uicolor Yellowcolor];    [Self.window Addsubview:_yearlabel];        [_yearlabel release]; Set month _monthlabel = [[UILabel alloc]initwithframe:cgrectmake (120, 50, 50,50)];    _monthlabel.text = @ "00";    _monthlabel.textcolor = [Uicolor Redcolor];    _monthlabel.textalignment = Nstextalignmentcenter;    _monthlabel.font = [Uifont fontwithname:@ "LCDD" size:30];    _monthlabel.backgroundcolor = [Uicolor Yellowcolor];    [Self.window Addsubview:_monthlabel];        [_monthlabel release];    Set Day _daylabel = [[UILabel alloc]initwithframe:cgrectmake (220, 50, 50, 50)];    _daylabel.text = @ "00";    _daylabel.textcolor = [Uicolor Redcolor];    _daylabel.textalignment = Nstextalignmentcenter;    _daylabel.font = [Uifont fontwithname:@ "LCDD" size:30];    _daylabel.backgroundcolor = [Uicolor Yellowcolor];    [Self.window Addsubview:_daylabel];            [_daylabel release];    Set UIView * view = [[UIView alloc]initwithframe:cgrectmake (0, 120, 70, 50)];    View.backgroundcolor = [Uicolor Yellowcolor];    [Self.window Addsubview:view];        [View release];    _hourlabel = [[UILabel alloc]initwithframe:cgrectmake (30, 0, 40, 50)]; _hourlabeL.text = @ "00";    _hourlabel.textcolor = [Uicolor Redcolor];    _hourlabel.textalignment = Nstextalignmentcenter;    _hourlabel.font = [Uifont fontwithname:@ "LCDD" size:30];    _hourlabel.backgroundcolor = [Uicolor Yellowcolor];    [View Addsubview:_hourlabel];        [_hourlabel release];    Set Sub _minutelabel = [[UILabel alloc]initwithframe:cgrectmake (120, 120, 50, 50)];    _minutelabel.text = @ "00";    _minutelabel.textcolor = [Uicolor Redcolor];    _minutelabel.textalignment = Nstextalignmentcenter;    _minutelabel.font = [Uifont fontwithname:@ "LCDD" size:30];    _minutelabel.backgroundcolor = [Uicolor Yellowcolor];    [Self.window Addsubview:_minutelabel];        [_minutelabel release];    Set seconds _secondlabel = [[UILabel alloc]initwithframe:cgrectmake (220, 120, 50, 50)];    _secondlabel.text = @ "00";    _secondlabel.textcolor = [Uicolor Redcolor];    _secondlabel.textalignment = Nstextalignmentcenter;    _secondlabel.font = [Uifont fontwithname:@ "LCDD" size:30]; _seconDlabel.backgroundcolor = [Uicolor Yellowcolor];    [Self.window Addsubview:_secondlabel];            [_secondlabel release]; }


Furthermore, we get the time now ...

Get time, return string-(NSString *) getdate{    nsdate * date = [NSDate date];    NSDateFormatter * DateFormat = [[NSDateFormatter alloc]init];    [DateFormat setdateformat:@ "yyyy mm DD HH mm SS"];    Returns the formatted time string return    [DateFormat stringfromdate:date];}

yyyy mm DD HH mm SS is the style shown in the numbers .... In the then. We assign the parameters to the current time, and show ... The code is as follows <pre name= "code" class= "OBJC" >-(void) starttimer{    _timer = [Nstimer scheduledtimerwithtimeinterval:1 Target:self selector: @selector (changetime) Userinfo:nil Repeats:yes];}

-(void) changetime{    nsstring * string = [self getDate];    Nsarray * Datearray = [string componentsseparatedbystring:@ ""];    _yearlabel.text = [Datearray objectatindex:0];    _monthlabel.text = [Datearray objectatindex:1];    _daylabel.text = [Datearray objectatindex:2];    _hourlabel.text = [Datearray objectatindex:3];    _minutelabel.text = [Datearray objectatindex:4];    _secondlabel.text = [Datearray objectatindex:5];    }



The results are shown below:
















Implementation of the iOS digital clock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.