iOS的UIButton和UILable

1 篇文章 0 订阅
1 篇文章 0 订阅

初始化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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值