以编程方式创建的UIButton不响应

时间:2014-05-13 18:28:17

标签: ios uibutton

我以编程方式创建两个UIButtons作为包含动画条形图的两个单独UIViews的子视图。图表由计算值驱动。在动画结束时,每个按钮都显示在其相应栏的顶部。看起来像这样:

enter image description here

按钮的用途(由屏幕截图中的黑色选框指示)用于显示信息,当点击时,将显示到详细视图控制器,其中附加信息显示在tableview中。

我想根据它的控制状态更改按钮上标题文字的颜色,这样当它被点击时,它会改变颜色以指示选择。我的问题的最初症状是文本颜色在点击时没有改变。

所以我做了一个小测试应用来比较在不同设置中以编程方式创建的UIButton的行为。测试应用程序的按钮创建和实现代码基本上与原始应用程序中的相同,减去算术。我的观察是它似乎在测试应用程序中运行良好。

我的后续测试表明原始应用中的按钮根本没有响应。喜欢死的东西。

这是原始项目的相关代码。我为这个长度道歉,而且我很清楚SO不是一个校对服务,但我对这个问题可能是什么感到遗憾:

-(void) makeBarChart
{
    RiserBar *thisRiser;
    firstLabel = [[UIButton alloc] initWithFrame:CGRectZero];
    secondLabel = [[UIButton alloc] initWithFrame:CGRectZero];

    // Bar colors

    UIColor *startColor1;
    UIColor *endColor1;
    UIColor *startColor2;
    UIColor *endColor2;

    if (goButtonKey == 1)
    {
        startColor1 = [UIColor blackColor];
        endColor1 = [UIColor redColor];
        startColor2 = [UIColor blackColor];
        endColor2 = [UIColor blueColor];
    }

    else if (goButtonKey == 2)
    {
        startColor1 = [UIColor blackColor];
        endColor1 = Rgb2UIColor(253, 165, 1);
        startColor2 = [UIColor blackColor];
        endColor2 = Rgb2UIColor(26, 138, 1);
    }
    else if (goButtonKey == 0)
    {
        startColor1 = [UIColor blackColor];
        endColor1 = [UIColor greenColor];
        startColor2 = [UIColor blackColor];
        endColor2 = [UIColor greenColor];
    }

// Here's the entry point if I have more than 2 risers

for(int i=0; i<2; i++)
{
    thisRiser = [[RiserBar alloc] initWithFrame:CGRectZero];
    thisRiser.tag = i+1;
    [self.chartView addSubview:thisRiser];
}

    // Calculate final dimensions of risers
int barWidth = self.chartView.bounds.size.width /((2 * 2) -1);
int focusBarEndHeight = (self.chartView.bounds.size.height-80) * (focusItemPercent / 100);
int benchmarkBarEndHeight = (self.chartView.bounds.size.height-80) * (benchmarkItemPercent / 100);



for (thisRiser in self.chartView.subviews)
{
    switch (thisRiser.tag)
    {
        case 1:
        {
            [thisRiser addSubview:firstLabel];
            [firstLabel setHidden:YES];


            [UIView animateWithDuration:1
                                  delay:.2
                                options: UIViewAnimationCurveEaseOut
                             animations:^
             {
                 thisRiser.frame = CGRectMake(35, self.chartView.frame.size.height, barWidth, 0);
                 thisRiser.backgroundColor = startColor1;
                 thisRiser.frame = CGRectMake(35, self.chartView.frame.size.height, barWidth, -focusBarEndHeight);
                 thisRiser.backgroundColor = endColor1;
                 thisRiser.layer.shadowColor = [[UIColor blackColor] CGColor];
                 thisRiser.layer.shadowOpacity = 0.7;
                 thisRiser.layer.shadowRadius = 4.0;
                 thisRiser.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
                 thisRiser.layer.shadowPath = [UIBezierPath bezierPathWithRect:thisRiser.bounds].CGPath;
             }
                             completion:^(BOOL finished)
             {
                 firstLabel.frame = CGRectMake(thisRiser.bounds.origin.x, thisRiser.frame.size.height -focusBarEndHeight - 55, barWidth, 60);
                 [firstLabel.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
                 firstLabel.titleLabel.numberOfLines = 0;
                 firstLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
                 [firstLabel setTitle:[NSString stringWithFormat:@"%@\n%.2f%%\nTime--%@",focusItemName,focusItemPercent,actualDurationFocusItem] forState:UIControlStateNormal];
                 firstLabel.backgroundColor = [UIColor clearColor];
                 [firstLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                 [firstLabel setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
                 [firstLabel setHidden:NO];
                 [firstLabel addTarget:self action:@selector(firstLabelAction:) forControlEvents:UIControlEventTouchUpInside];
                 [firstLabel setUserInteractionEnabled:YES];
                 [thisRiser setUserInteractionEnabled:YES];


                 NSLog(@"Done!");
             }];
        }
            break;

        case 2:

        {
            [thisRiser addSubview:secondLabel];
            [secondLabel setHidden:YES];


            [UIView animateWithDuration:1
                                  delay:.2
                                options: UIViewAnimationCurveEaseOut
                             animations:^
             {
                 thisRiser.frame = CGRectMake(barWidth + 70, self.chartView.frame.size.height, barWidth, 0);
                 thisRiser.backgroundColor = startColor2;
                 thisRiser.frame = CGRectMake(barWidth + 70, self.chartView.frame.size.height, barWidth, -benchmarkBarEndHeight);
                 thisRiser.backgroundColor = endColor2;
                 thisRiser.layer.shadowColor = [[UIColor blackColor] CGColor];
                 thisRiser.layer.shadowOpacity = 0.7;
                 thisRiser.layer.shadowRadius = 4.0;
                 thisRiser.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
                 thisRiser.layer.shadowPath = [UIBezierPath bezierPathWithRect:thisRiser.bounds].CGPath;

             }
                             completion:^(BOOL finished)
             {
                 secondLabel.frame = CGRectMake(thisRiser.bounds.origin.x, thisRiser.frame.size.height -benchmarkBarEndHeight - 55, barWidth, 60);
                 [secondLabel setTitle:[NSString stringWithFormat:@"%@\n%.2f%%\nTime--%@",benchmarkItemName,benchmarkItemPercent,actualDurationBenchmarkItem] forState:UIControlStateNormal];
                 [secondLabel.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
                 secondLabel.titleLabel.numberOfLines = 0;
                 secondLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
                 [secondLabel setHidden:NO];
                 secondLabel.backgroundColor = [UIColor clearColor];
                 [secondLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                 [secondLabel setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
                 [secondLabel addTarget:self action:@selector(secondLabelAction:) forControlEvents:UIControlEventTouchUpInside];
                 [secondLabel setUserInteractionEnabled:YES];
                 [thisRiser setUserInteractionEnabled:YES];


                 NSLog(@"Done!");
             }];
        }
            break;

        default:
            break;
    }
}
}

-(IBAction)firstLabelAction:(UIButton*)sender

{
    [self performSegueWithIdentifier:@"activityDetailPush" sender:firstLabel];
}


-(void) secondLabelAction:(UIButton*)sender
{
    [self performSegueWithIdentifier:@"categoryDetailPush" sender:secondLabel];
}

谢谢你的期待。所有帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

也许这可以解决它。

UIButton *firstLabel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
firstLabel.frame = CGRectZero;
UIButton *secondLabel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
secondLabel.frame = CGRectZero;