Initializes the UIButton and Uilable objects, and then sets the button's Click event Listener to change the value of the uilable to the current time;
Run:
The code is as follows:
////VIEWCONTROLLER.M//tableviewdemo04////Created by Vrinux on 15/6/4.//Copyright (c) 2015 Vrinux. All rights reserved.//#import "ViewController.h" @interface viewcontroller ()//define button;@property(nonatomic,Strong)UIButton*mbutton;//define label;@property(nonatomic,Strong)UILabel*mlable;@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];additional setup after loading the view, typically from a nib.//Initialize button;_mbutton = [UIButtonButtonwithtype:uibuttontypecustom];//Draw the size of the button;_mbutton. Frame= CGRectMake (100.0F100.0F200.0F100.0f);//Set background color;[_mbutton setbackgroundcolor:[UicolorRedcolor]];//Set button text;[_mbutton settitle:@"Click on Me."Forstate:uicontrolstatenormal];//Set button click event, call function OnClick:[_mbutton AddTarget: SelfAction@selector(OnClick:) forcontrolevents:uicontroleventtouchupinside];//Add the button to the view;[ Self. ViewAddsubview:_mbutton];//initialization of uilable;_mlable = [[UILabelAlloc] Initwithframe:cgrectmake ( -, -, $, -)];//Set font size;_mlable. Font= [UifontBoldsystemfontofsize:18.0F];//Set font alignment;_mlable. TextAlignment= Nstextalignmentcenter;//Set text color;_mlable. TextColor= [UicolorWhitecolor];//Set background color;[_mlable setbackgroundcolor:[UicolorRedcolor]];//Add the button to the view;[ Self. ViewAddsubview:_mlable];} - (void) Didreceivememorywarning {[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}//Set the method to invoke the Click event;- (void) OnClick: (ID) senser{_mlable. Text= [ SelfGetDate];}//Get the current time;- (NSString*) getdate{NSDate* senddate=[NSDateDate]; NSDateFormatter *dateformatter=[[nsdateformatter alloc] init]; [Dateformatter setdateformat:@"Yyyy-mm-dd HH:mm:ss"];NSString* Locationstring=[dateformatter Stringfromdate:senddate];returnlocationstring;}@end
iOS UIButton and uilable