#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