别人的批量创建UIButton

.h
//
//  UIButton+Create.h
//
//  Created by 魏建科 on 13-7-25.
//  Copyright (c) 2013年 魏建科. All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
@interface UIButton (Create)
+ (UIButton*) createButtonWithFrame: (CGRect) frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed;
+ (UIButton *) createButtonWithFrame:(CGRect)frame Title:(NSString *)title Target:(id)target Selector:(SEL)selector;
 
@end
 
.m
//
//  UIButton+Create.m
//
//  Created by 魏建科 on 13-7-25.
//  Copyright (c) 2013年 魏建科. All rights reserved.
//
 
#import "UIButton+Create.h"
 
@implementation UIButton (Create)
 
+ (UIButton*) createButtonWithFrame: (CGRect) frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    UIImage *newImage = [UIImage imageNamed: image];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
    UIImage *newPressedImage = [UIImage imageNamed: imagePressed];
    [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return button;
}
 
+ (UIButton *) createButtonWithFrame:(CGRect)frame Title:(NSString *)title Target:(id)target Selector:(SEL)selector
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return button;
}
 
@end
 
使用方法,首先在要使用的ViewController
包含#import "UIButton+Create.h"
然后使用方法
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:[UIButton createButtonWithFrame:CGRectMake(0, 0, 160, 40) Title:@"测试" Target:self Selector:@selector(buttonPressed:)]];
    [self.view addSubview:[UIButton createButtonWithFrame:CGRectMake(0, 100, 160, 40) Target:self Selector:@selector(buttonPressed:) Image:@"这个是你给这个Button设置的默认图片" ImagePressed:@"button点击后显示的高亮的图片"]];
}
 
- (void)buttonPressed:(id)sender
{
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值