我正在为tableView使用customCell.CustomCell包含UIButton.And我想用这个按钮分配一个动作。
BeginingCell.h将UIButton的偏差保持为:
#import <UIKit/UIKit.h>
@interface BeginingCell : UITableViewCell {
IBOutlet UIButton *ansBtn1;
}
@property(nonatomic,retain)IBOutlet UIButton *ansBtn1;
@end
实施是:
//
// BeginingCell.m
// customCells
//
#import "BeginingCell.h"
@implementation BeginingCell
///########### Synthesize ///###########
@synthesize ansBtn1;
///########### Factory ///###########
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
///########### End Factory ///###########
- (void)dealloc {
[super dealloc];
}
@end
我在tableView中使用这个CustomCell。我希望从按钮Click响应。如何为我的customCell Button分配按钮操作?
答案 0 :(得分:1)
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self
action:@selector(myButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:@"Action" forState:UIControlStateNormal];
myButton.frame = CGRectMake(buttonFrame);
[self addSubview:myButton];
答案 1 :(得分:0)
不要将您的按钮设为IBOutlet并尝试将其写入您的IBAction,而只需在-(void)viewDidLoad
中手动连接它,如下所示:
[ansBtn1 addTarget:self action: @selector(myActionMethod:)
forControlEvents:UIControlEventTouchUpInside];
答案 2 :(得分:0)
您可以使用addTarget:action:forControlEvents:
方法(UIControl的方法,UIButton是子类)以编程方式向UIButton添加操作。
所以你的实现看起来像是:
[ansBtn1 addTarget:self
action:@selector(btnAction)
forControlEvents:UIControlEventTouchUpInside];
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[ansBtn1 addTarget:self
action:@selector(btnAction:)
forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
//Buttons Action
-(void) btnAction:(id) sender
{
}