iOS的UIButton和UILable
初始化UIButton和UILable对象,然后设置按钮的点击事件监听,改变UILable的值为当前时间;
运行截图:
代码如下:
//
// ViewController.m
// tableviewdemo04
//
// Created by vrinux on 15/6/4.
// Copyright (c) 2015年 vrinux. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
// 定义按钮;
@property (nonatomic,strong) UIButton *mButton;
// 定义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.
// 初始化按钮;
_mButton = [UIButton buttonWithType:UIButtonTypeCustom];
// 绘制按钮的尺寸;
_mButton.frame = CGRectMake(100.0f, 100.0f, 200.0f, 100.0f);
// 设置背景颜色;
[_mButton setBackgroundColor:[UIColor redColor]];
// 设置按钮文字;
[_mButton setTitle:@"点击我吧" forState:UIControlStateNormal];
// 设置按钮的点击事件,调用函数 onClick:
[_mButton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
// 将按钮添加到view上面;
[self.view addSubview:_mButton];
// 初始化UILable;
_mLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 350, 200, 100)];
// 设置字体大小;
_mLable.font = [UIFont boldSystemFontOfSize:18.0f];
// 设置字体对齐方式;
_mLable.textAlignment = NSTextAlignmentCenter;
// 设置文字颜色;
_mLable.textColor = [UIColor whiteColor];
// 设置背景颜色;
[_mLable setBackgroundColor:[UIColor redColor]];
// 将按钮添加到view上面;
[self.view addSubview:_mLable];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 设置点击事件调用的方法;
- (void)onClick:(id)senser{
_mLable.text = [self getDate];
}
// 获取当前时间;
- (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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。