iOS UIButton and uilable

Source: Internet
Author: User

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

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.