UIButton 嵌入 UIVIew 不负责任

Posted

技术标签:

【中文标题】UIButton 嵌入 UIVIew 不负责任【英文标题】:UIButton embeded in UIVIew no responsible 【发布时间】:2017-06-04 08:47:22 【问题描述】:

我有问题

我创建了 6 个 UIButtons 将它们放入 UIView1

然后将UIView1 放入另一个UIView2

然后将UIView2作为UITableView.tableHeaderView返回

现在问题是当我触摸按钮时

不负责任

这里有一些信息 1. UIButtons UIView1 UIView2 fremes 是这些

    2017-06-04 16:26:40.014 LOLHelper[8590:223132] BUT frame is :8.000000 8.000000 70.500000 18.000000
2017-06-04 16:26:40.014 LOLHelper[8590:223132] BUT frame is :86.500000 8.000000 71.000000 18.000000
2017-06-04 16:26:40.014 LOLHelper[8590:223132] BUT frame is :165.500000 8.000000 70.500000 18.000000
2017-06-04 16:26:40.015 LOLHelper[8590:223132] BUT frame is :244.000000 8.000000 70.500000 18.000000
2017-06-04 16:26:40.015 LOLHelper[8590:223132] BUT frame is :322.500000 8.000000 71.000000 18.000000
2017-06-04 16:26:40.015 LOLHelper[8590:223132] BUT frame is :401.500000 8.000000 70.500000 18.000000
2017-06-04 16:26:40.015 LOLHelper[8590:223132] SubView frame is: :-3.000000 0.000000 480.000000 20.000000
2017-06-04 16:26:40.015 LOLHelper[8590:223132] headerView frame is: :0.000000 0.000000 320.000000 20.000000

2.我打开UIButtonsUIView1UIView2userInteractionEnabled,你可以在代码中看到:

    #import "TableViewDataSource.h"


@implementation TableViewDataSource

@synthesize tableView;
@synthesize tableCell;
@synthesize LHfetchedResultsController;
@synthesize numberOfRows;
@synthesize dataTemp;
@synthesize paused;
@synthesize type;
@synthesize imgAche;
@synthesize cellHeight;
@synthesize subHeaderViewFrame;

-(id)initWithTableView:(UITableView *) tableView

    self = [super init];
    if (self) 
        self.tableView = tableView;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.tableHeaderView = [self setHeaderView];
    
    return self;

-(UIView *)setHeaderView

    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UIView * container = [[UIView alloc] init];
    container.frame = CGRectMake(0, 0, screenSize.width, 20);
    container.userInteractionEnabled = YES;

    UIView * headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, screenSize.width*1.5, 20);
    headerView.tag = 01;
    headerView.userInteractionEnabled = YES;
    [container addSubview:headerView];
    for (int i = 1; i < 7; i++) 
        UIButton * button = [[UIButton alloc] init];
        button.tag = i+10;
        button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
        [button setTitle:[NSString stringWithFormat:@"but0%d",i] forState:UIControlStateNormal];
        [button setTitle:[NSString stringWithFormat:@"but0%d s",i] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.translatesAutoresizingMaskIntoConstraints = NO;
        [button setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
        button.enabled = YES;
        button.userInteractionEnabled = YES;
        [headerView addSubview:button];
    
    UIButton *button01 = [headerView viewWithTag:11];
    UIButton *button02 = [headerView viewWithTag:12];
    UIButton *button03 = [headerView viewWithTag:13];
    UIButton *button04 = [headerView viewWithTag:14];
    UIButton *button05 = [headerView viewWithTag:15];
    UIButton *button06 = [headerView viewWithTag:16];

    NSDictionary * views = NSDictionaryOfVariableBindings(button01,button02,button03,button04,button05,button06);
    NSString * vflV1 = @"V:|-1-[button01]-1-|";
    NSString * vflV2 = @"V:|-1-[button02]-1-|";
    NSString * vflV3 = @"V:|-1-[button03]-1-|";
    NSString * vflV4 = @"V:|-1-[button04]-1-|";
    NSString * vflV5 = @"V:|-1-[button05]-1-|";
    NSString * vflV6 = @"V:|-1-[button06]-1-|";
    NSString * vflH1 = @"H:|-[button01]-[button02(==button01)]-[button03(==button01)]-[button04(==button01)]-[button05(==button01)]-[button06(==button01)]-|";

    NSArray * constraintsV1 = [NSLayoutConstraint constraintsWithVisualFormat:vflV1 options:0 metrics:NULL views:views];
    NSArray * constraintsV2 = [NSLayoutConstraint constraintsWithVisualFormat:vflV2 options:0 metrics:NULL views:views];
    NSArray * constraintsV3 = [NSLayoutConstraint constraintsWithVisualFormat:vflV3 options:0 metrics:NULL views:views];
    NSArray * constraintsV4 = [NSLayoutConstraint constraintsWithVisualFormat:vflV4 options:0 metrics:NULL views:views];
    NSArray * constraintsV5 = [NSLayoutConstraint constraintsWithVisualFormat:vflV5 options:0 metrics:NULL views:views];
    NSArray * constraintsV6 = [NSLayoutConstraint constraintsWithVisualFormat:vflV6 options:0 metrics:NULL views:views];
    NSArray * constraintsH1 = [NSLayoutConstraint constraintsWithVisualFormat:vflH1 options:0 metrics:NULL views:views];

    [headerView addConstraints:constraintsV1];
    [headerView addConstraints:constraintsV2];
    [headerView addConstraints:constraintsV3];
    [headerView addConstraints:constraintsV4];
    [headerView addConstraints:constraintsV5];
    [headerView addConstraints:constraintsV6];
    [headerView addConstraints:constraintsH1];

    return container;

我还没找到原因

谁能帮帮我?

我还在UIView2中添加了一些UISwipeGestureRecognizer

谢谢你

【问题讨论】:

可能有一个view覆盖了你的tableview的header部分,检查一下。 我的结构是这样的:self.tableView.tableHeaderView包含一个UIView,然后UIView包含6个UIButtons,关于这些的所有代码都在那里,我可以在代码中成功获取按钮,所以我不确定是否还有其他我没有注意到的观点,你能告诉我它可能是什么观点吗? 是否有一个视图包含tableView?该容器视图中还有其他视图吗? 如果你这样做NOTUISwipeGestureRecognizer 添加到UIView2 你可以点击按钮吗?如果是这样,那么你已经找到了问题。 @YunCHEN 我没有在容器视图中添加另一个UIView,并且tableView 是插入主ViewController,我可以触摸UITableViewCell 【参考方案1】:

谢谢你们 我得到了解决方案 原因是 [button addTarget:NULL action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; 我将目标设置为NULL,并在同一个文件中写入click:,但实际上我是从主控制器获取表格,所以当我点击按钮时,xcode会在主控制器中找到click:,然后找不到,所以没有责任。 在我设置目标自我或在主控制器中写入click: 它有效

【讨论】:

以上是关于UIButton 嵌入 UIVIew 不负责任的主要内容,如果未能解决你的问题,请参考以下文章

UC浏览器 - 不负责任思考

“爆到天际线” - TiDB 2021 Hackathon 决赛不负责任点评

不负责任的聊下 Apache Doris

看似不负责任的菩提祖师,却用另外一种方式,造就了孙悟空的人生

看似不负责任的菩提祖师,却用另外一种方式,造就了孙悟空的人生

看似不负责任的菩提祖师,却用另外一种方式,造就了孙悟空的人生