码迷,mamicode.com
首页 > 移动开发 > 详细

iOS的UIButton和UILable

时间:2015-06-04 17:15:46      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:ios   uibutton   uilabel   点击事件监听   

初始化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

iOS的UIButton和UILable

标签:ios   uibutton   uilabel   点击事件监听   

原文地址:http://blog.csdn.net/vrinux/article/details/46362503

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!