iOS 中添加按钮和按钮监听方法
9月 1, 2014
代码添加按钮和按钮监听方法
// ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)tapButton:(UIButton *)sender{
NSLog(@"我是%@",[sender.titleLabel text]);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(100, 100, 200, 200)];
[button setTitle:@"hello" forState:UIControlStateNormal];
[button setTitle:@"world" forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
[button setBounds:CGRectMake(200, 200, 200, 200)];
[button setBackgroundColor:[UIColor grayColor]];
UIImage *image = [UIImage imageNamed:@"photo/12.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
[self.view addSubview:button];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end