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

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

Sep 1, 2014
Objc, Xcode

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

//  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
本文共 149 字,上次修改于 Jul 30, 2022,以 CC 署名-非商业性使用-禁止演绎 4.0 国际 协议进行许可。

相关文章

» iOS 中 textFieldShouldReturn 方法的使用