iOS 中添加按钮和按钮监听方法

iOS 中添加按钮和按钮监听方法

Sep 1, 2014
Objc

代码添加按钮和按钮监听方法

//  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
本文以 CC 署名-非商业性使用-禁止演绎 4.0 国际 协议进行许可。

上一篇:iOS 中 textFieldShouldReturn 方法的使用

下一篇:这个博客