您的位置:首页 > 产品设计 > UI/UE

IOS开发(16)之UIButton控件

2013-04-24 15:57 495 查看

1 前言

UIButton为按钮控件,在IOS开发中十分常见,可以为其设置事件。

2 代码实例

ZYViewController.m:

@synthesize myButton;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];//设置按钮样式为圆角矩形
myButton.frame = CGRectMake(110.0f, 200.0f, 100.0f, 37.0f);
[myButton setTitle:@"Press Me" forState:UIControlStateNormal];//按下按钮时候的标题
[myButton setTitle:@"I'm Pressed" forState:UIControlStateHighlighted];//按下按钮后抬手时候的标题
[myButton addTarget:self action:@selector(buttonIsPressed:) forControlEvents:UIControlEventTouchDown];//按下按钮触发事件
[myButton addTarget:self action:@selector(buttonIsTapped:) forControlEvents:UIControlEventTouchUpInside];//按下按钮后抬手时候触发事件
[self.view addSubview:myButton];
}

-(void)buttonIsPressed:(UIButton *)paramSender{
NSLog(@"Button is pressed");
}

-(void)buttonIsTapped:(UIButton *)paramSender{
NSLog(@"Button is tapped");
}


运行结果:



点击按钮时候:



控制台输出:

2013-04-24 16:00:13.549 UIButtonTest[1635:c07] Button is pressed
2013-04-24 16:00:13.623 UIButtonTest[1635:c07] Button is tapped
ZYUIButtonViewController.m:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];//设置按钮样式为圆角矩形
myButton.frame = CGRectMake(110.0f, 200.0f, 100.0f, 37.0f);
[myButton setBackgroundImage:[UIImage imageNamed:@"ok_btn_normal.png"] forState:UIControlStateNormal];//正常状态下时候的标题
[myButton setBackgroundImage:[UIImage imageNamed:@"ok_btn_pressed.png"] forState:UIControlStateHighlighted];//按下按钮时候的标题
[self.view addSubview:myButton];
}


运行结果:



点击按钮时候:



3 结语

以上是对UIbutton的简单介绍希望对大家有所帮助。

Demo实例下载:http://download.csdn.net/detail/u010013695/5297459
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息