UIKit--UIButton的简单应用

#import <UIKit/UIKit.h>


@interface RootViewController : UIViewController

{

@private

    UILabel * label;//标签,用于显示字符串

    UIButton * button;//按钮,用于点击,产生交互事件

}


@end




#import "RootViewController.h"


@implementation RootViewController


-(void)onClick

{

    static int cnt;

    label.text = [NSString stringWithFormat:@"点了%d", ++cnt];

    return;

}



- (void)viewDidLoad

{

    [super viewDidLoad];

    

    label = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 80)];

    label.text = @"这里显示点击次数";

    label.font = [UIFont systemFontOfSize:50];

    label.adjustsFontSizeToFitWidth = YES;

    label.textColor = [UIColor redColor];

    

    [self.view addSubview:label];

    /*各种button的样式

     UIButtonTypeCustom = 0,           

     UIButtonTypeRoundedRect,     

     UIButtonTypeDetailDisclosure,

     UIButtonTypeInfoLight,

     UIButtonTypeInfoDark,

     UIButtonTypeContactAdd,

    */

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    //创建圆角矩形button

    button.frame = CGRectMake(110, 200, 100, 40);

    

    [button setTitle:@"点这里" forState:UIControlStateNormal];

    //正常状态下显示标题点这里,各种状态是枚举

    

    [button setTitle:@"点了" forState:UIControlStateHighlighted];

    //高亮状态(即点击时的状态)显示点了

    

    button.titleLabel.font = [UIFont systemFontOfSize:30];

    //button里面有个成员是label,可以做label做的所有设置

    

    [button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];

    //添加事件,当点击这个button [self onClick],触发事件是点击后抬起手

    

    [self.view addSubview:button];

    //黏贴button到画布

    

}



- (void)viewDidUnload

{

    [super viewDidUnload];

        

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


-(void)dealloc

{

    [label release];

    [super dealloc];

    return;

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值