Button在tableFooterView上创建不响应事件

时间:2016-06-01 02:17:34

标签: ios objective-c uitableview

我在从UIView继承的类中创建了一个tableView。当我在tableView上的tableFooterView上添加一个按钮时,它运行良好。

CustomView.h
#import <UIKit/UIKit.h>
@interface CustomView : UIView<UITableViewDelegate,UITableViewDataSource>
@end

CustomView.m
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 80)];
    UIButton * tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    tempButton.center = customView.center;
    tempButton.backgroundColor = [UIColor redColor];
    [tempButton addTarget:self action:@selector(doButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [customView addSubview:tempButton];

    return customView;
}

- (void)doButtonAction{
    NSLog(@"123");
}

的NSLog:123

但是,我公开了这个按钮并在我的控制器中进行了一个目标动作。它没有任何反应。

CustomView.h
@property (strong, nonatomic) UIButton * tempButton;

CustomView.m
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 80)];
    self.tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    self.tempButton.center = customView.center;
    [customView addSubview:self.tempButton];
    return customView;
}

ViewController.h

- (void)viewDidLoad {
    [super viewDidLoad];
    CustomView *cv = [[CustomView alloc] initWithFrame:self.view.bounds ];
    [cv.tempButton addTarget:self action:@selector(doButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cv];
}
- (void)doButtonAction{
    NSLog(@"123");
}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

viewdidload中你没有在按钮上设置任何东西, 您只需在@interface

中进行分配

但您必须在backgroundcolor的按钮上分配并设置properties和所有viewdidload

之后它会起作用。

可能会帮助您或感到自由。