UIButton突然停止工作

时间:2013-11-21 15:47:22

标签: ios iphone objective-c uibutton

所以我的照片应用中的过滤器按钮出现问题。我不确定为什么,但他们突然停止了工作。选择器在被点击时没有被调用,但我不知道为什么。他们几天前工作得很好,但出于某种原因,他们现在还没有。我检查了UIView子视图数组,验证它是正确的。我需要一种方法来看看,由于某种原因,触摸是不是按钮。我不知道该怎么做。

我希望我能提供更多信息,但这就是我所拥有的。我希望有人有一些建议,因为我已经结束了它。

提前致谢!

按钮创建方法:

-(void)createFilterButtonsForThumbnail:(UIImage *)thumbnail
{
    UIView *filtersContainer = self.filterContainer;
    CGRect buttonFrame = CGRectMake(kFilterFrameThickness, kFilterFrameThickness,
                                    thumbnail.size.width, thumbnail.size.height);

    CGFloat frameWidth = thumbnail.size.width+(2*kFilterFrameThickness);
    CGFloat frameHeight = kFilterPickerHeight;

    UIEdgeInsets backgroundInsets = UIEdgeInsetsMake(0, kFilterFrameThickness, 0, kFilterFrameThickness);
    UIImage *buttonBackgroundImage = [[UIImage imageNamed:@"FilmReel"] resizableImageWithCapInsets:backgroundInsets];

    for (int i = 0;i<(self.filterPaths.count+kFilterNonLookups+1);i++){


        UIImageView *buttonBackground = [[UIImageView alloc] initWithFrame:CGRectMake(kFilterSidePadding+(i*(frameWidth+kFilterSidePadding)),
                                                                            0,
                                                                            frameWidth,
                                                                            frameHeight)];
        [buttonBackground setImage:buttonBackgroundImage];

        UIButton *thumbnailButton = [[UIButton alloc] initWithFrame:buttonFrame];
        UIImage *filteredThumbnail = [self applyFilterAtIndex:i ToImage:thumbnail];

        [thumbnailButton setImage:filteredThumbnail forState:UIControlStateNormal];
        [thumbnailButton addTarget:self action:@selector(filterSelected:) forControlEvents:UIControlEventTouchUpInside];
        [thumbnailButton setTag:i];

        [buttonBackground addSubview:thumbnailButton];
        [filtersContainer addSubview:buttonBackground];
        if ((i > (kFilterProMinimumIndex)) && ([self isProVersion]) == NO){
            UIImageView *proTag = [[UIImageView alloc] initWithImage:nil];
            CGRect proFrame = CGRectMake(buttonFrame.origin.x,
                                         buttonFrame.origin.y + buttonFrame.size.height - kFilterProIconHeight-kFilterFrameThickness,
                                         kFilterProIconWidth,
                                         kFilterProIconHeight);
            [proTag setBackgroundColor:[UIColor orangeColor]];
            [proTag setFrame:proFrame];
            [thumbnailButton addSubview:proTag];
            [self.filterProTags addObject:proTag];
        }
    }
}

选择器方法:

-(void)filterSelected:(UIButton *)button
{
    NSLog(@"Pressed button for index %i",button.tag);
    int buttonTag = button.tag;
    if ((buttonTag < kFilterProMinimumIndex+1) || ([self isProVersion] == YES)){
        [self.imageView setImage:[self applyFilterAtIndex:buttonTag ToImage:self.workingImage]];
    }
    else {
        [self processProPurchaseAfterAlert];
    }
}

1 个答案:

答案 0 :(得分:4)

我在这里看到了几个问题,但我不确定是哪些问题导致它破裂。首先,您应该使用UIButton上的buttonWithType:实例化您的按钮:

UIButton *thumbnailButton = [UIButton buttonWithType:UIButtonTypeCustom];
[thumbnailButton setFrame:buttonFrame]

原因是UIButton是一个类集群,你应该让操作系统给你正确的按钮。

其次,您将Button添加为UIImageView的子视图,默认情况下,userInteractionEnabled设置为NO

  

此属性继承自UIView父类。这个班   将此属性的默认值更改为NO。

你应该让按钮成为一个大按钮,按钮的背景是你想要的图像。