您的位置:首页 > 移动开发 > IOS开发

iOS_自定义Button

2015-10-17 09:07 295 查看
自定义Button

//
//  CustomButton.h
//  自定义 button
//
//  Created by Qianfeng on 15/10/10.
//  Copyright (c) 2015年 xyz. All rights reserved.
//

/**
*  定制Button
*/

#import <UIKit/UIKit.h>

@class CustomButton;
typedef void (^ButtonBlock)(CustomButton *button);

@interface CustomButton : UIButton

- (void)addClickEventTouchUpInside:(ButtonBlock)block;

@end


//
//  CustomButton.m
//  自定义 button
//
//  Created by Qianfeng on 15/10/10.
//  Copyright (c) 2015年 xyz. All rights reserved.
//

#import "CustomButton.h"

@interface CustomButton()
@property (nonatomic, copy) ButtonBlock block;
@end

@implementation CustomButton

- (void)addClickEventTouchUpInside:(ButtonBlock)block {
self.block = block;
[self addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
}
- (void)btnclick {
if (self.block) {
self.block(self);
}
}
@end
//
//  ViewController.h
//  自定义 button
//
//  Created by Qianfeng on 15/10/10.
//  Copyright (c) 2015年 xyz. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end


//
//  ViewController.m
//  自定义 button
//
//  Created by Qianfeng on 15/10/10.
//  Copyright (c) 2015年 xyz. All rights reserved.
//

#import "ViewController.h"
#import "CustomButton.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self customButton];
}
- (void)customButton {
CustomButton *btn = [[CustomButton alloc] initWithFrame:CGRectMake(20, 20, 100, 50)];
btn.backgroundColor = [UIColor redColor];
[btn setTitle:@"custombtn" forState:UIControlStateNormal];
[self.view addSubview:btn];

[btn addClickEventTouchUpInside:^(CustomButton *button) {
NSLog(@"点击了自定义 按钮");
}];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: