UIButton and UILable of iOS
Initialize the UIButton and UILable objects, set the click event listening button, and change the value of 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 () // defines the button; @ property (nonatomic, strong) UIButton * mButton; // defines the Label; @ property (nonatomic, strong) UILabel * mLable; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // initialization button; _ mButton = [UIButton buttonWithType: UIButtonTypeCustom]; // the size of the draw button; _ mButton. frame = CGRectMake (1000000f, 1000000f, 2000000f, 1000000f); // set the background color; [_ mButton setBackgroundColor: [UIColor redColor]; // set the button text; [_ mButton setTitle: @ "Click me" forState: UIControlStateNormal]; // set the Click Event of the button and call the onClick function: [_ mButton addTarget: self action: @ selector (onClick :) forControlEvents: UIControlEventTouchUpInside]; // Add the button to the view; [self. view addSubview: _ mButton]; // initialize UILable; _ mLable = [[UILabel alloc] initWithFrame: CGRectMake (100,350,200,100)]; // set the font size; _ mLable. font = [UIFont boldSystemFontOfSize: 18366f]; // set the font alignment; _ mLable. textAlignment = NSTextAlignmentCenter; // set the text color; _ mLable. textColor = [UIColor whiteColor]; // set the background color; [_ mLable setBackgroundColor: [UIColor redColor]; // Add the button to the view; [self. view addSubview: _ mLable];}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} // set the method for calling the click event;-(void) onClick :( id) senser {_ mLable. text = [self getDate];} // get the current time;-(NSString *) getDate {NSDate * senddate = [NSDate date]; NSDateFormatter * dateformatter = [[NSDateFormatter alloc] init]; [dateformatter setDateFormat: @ "YYYY-MM-dd HH: mm: ss"]; NSString * locationString = [dateformatter stringFromDate: senddate]; return locationString ;}@ end