我怎么知道我点击了哪个视图?为什么按钮没反应?

时间:2011-08-07 15:06:18

标签: ios view uibutton

我正在设计一个具有多个按钮栏的viewcontroller,每个栏都可以点击并显示内容视图。 http://i.stack.imgur.com/m5V4Q.png

当我点击按钮栏时,它的框架会扩大,你可以看到其中的内容(这是一个按钮)。

首先,一个条形按钮(320 * 30大小)和一个contentView是一个集合,它们是listview的子视图。几个listview使整个视图。

其次,当我点击它时,列表视图将从320 * 30扩展到320 * 180,其中的内容视图将从300 * 0扩展到300 * 130(20像素的边距)。然后我将contentsToBounds of contentview设置为Yes,以确保当帧高度为0时,contentview中的内容不会显示。

现在,点击可以完全按照我的意愿显示内容视图。但这是一个问题:我无法单击其中的按钮,我尝试将其userInteractionEnabled设置为yes。甚至我将contentview设置为用户定义,我将视图的userInteractionEnabled设置为yes并覆盖touchbegin函数以显示alertView。那些测试都失败了,没有发现任何反应。我检查并确定不应该有任何视图。

原因可能是什么?

列表视图的设置代码是:

        NSArray *array = [titleArray objectAtIndex:i];

    NSString *ShenSuoTitle = [array objectAtIndex:0];

    NSString *ShenSuoContent = [array objectAtIndex:1];

    UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(0, totalHeight, 320, ShenSuoViewHeight)];
    [listView setBackgroundColor:[UIColor blackColor]];
    [listView setTag:[[NSString stringWithFormat:@"%d",(i+1)] intValue]];
    [self.scrollView addSubview:listView];

    totalHeight = totalHeight + 1 + ShenSuoViewHeight;

    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
    [titleView setBackgroundColor:[UIColor whiteColor]];
    [titleView setTag:[[NSString stringWithFormat:@"%d1",i+1] intValue]];
    [listView addSubview:titleView];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTag:[[NSString stringWithFormat:@"%d2",i+1] intValue]];
    [btn setFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
    [btn addTarget:self action:@selector(reSetFrame:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:btn];

    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, 30, 30)];
    img.image = [UIImage imageNamed:@"list_ico.png"];
    [img setTag:[[NSString stringWithFormat:@"%d3",i+1] intValue]];
    [titleView addSubview:img];
    NSLog(@"img:%f,%f",img.frame.origin.y,img.frame.size.height);


    UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, 100, 25)];
    labTitle.textColor = [UIColor blackColor];
    labTitle.backgroundColor = [UIColor clearColor];
    labTitle.font = [UIFont boldSystemFontOfSize:15];
    labTitle.text = ShenSuoTitle;
    [titleView addSubview:labTitle];
    NSLog(@"labTitle:%f,%f",labTitle.frame.origin.y,labTitle.frame.size.height);

    //add a label for selected
    UILabel *selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(214, 14, 86, 21)];
    selectedLabel.textColor = [UIColor grayColor];
    //selectedLabel.alpha = 0.75;
    selectedLabel.backgroundColor = [UIColor clearColor];
    selectedLabel.text = @"All";
    selectedLabel.font = [UIFont boldSystemFontOfSize:15];
    [selectedLabel setTag:[[NSString stringWithFormat:@"%d5",i+1] intValue]];
    [titleView addSubview:selectedLabel];
    NSLog(@"selectedLabel:%f,%f",selectedLabel.frame.origin.y,selectedLabel.frame.size.height);

    UIView *content = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + ShenSuoViewHeight, 300, 0)];
    content.backgroundColor = [UIColor grayColor];

    UILabel *testLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    testLa.text = @"Label";

    UIButton *testBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    testBut.frame = CGRectMake(0, 40, 100, 30);
    testBut.titleLabel.textColor = [UIColor greenColor];
    testBut.titleLabel.text = @"Button";

    content.tag = [[NSString stringWithFormat:@"%d4",i+1] intValue];
    [content addSubview:testLa];
    [content addSubview:testBut];

    content.clipsToBounds = YES;

    [testLa release];

    [listView addSubview:content];


    [content release];
    [labTitle release];
    [img release];
    [titleView release];
    [listView release];

代码处理单击以使列表视图展开为:

    UIView *titleView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:@"%d1",i+1] intValue]];
    titleView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:0.9];

    UIView *listView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:@"%d",i+1] intValue]];
    listView.frame = CGRectMake(0, totalHeight, 320, ShenSuoViewHeight);
    totalHeight = totalHeight + 1 + 20 + 180 + ShenSuoViewHeight;


    UIView *content = [self.view viewWithTag:[[NSString stringWithFormat:@"%d4",i+1] intValue]];
    content.frame = CGRectMake(10, 10 + ShenSuoViewHeight, 300, 180);

    UIImageView *img = (UIImageView*)[self.view viewWithTag:[[NSString stringWithFormat:@"%d3",i+1] intValue]];
    img.image = [UIImage imageNamed:@"list_ico_d.png"];

2 个答案:

答案 0 :(得分:0)

我不确定我是否完全按照你的情况,有些代码会很好。尽管如此,回答你的问题的原因很可能是触摸被其他地方截获。也可能是您忘记将操作方法​​连接到按钮。尝试使用断点或NSLog来查看哪些方法被触发,哪些方法没有被触发。

答案 1 :(得分:0)

非常感谢。这是由于我的错。扩展设置列表视图框架的栏的代码是错误的,它应该在扩展高度中添加。这导致视觉效果正常,但点击不被处理。 所以这个问题关闭了。