ios - UIButton 突然停止工作

标签 ios iphone objective-c uibutton

所以我的照片应用程序中的滤镜按钮出现问题。我不确定为什么,但他们突然停止工作了。点击选择器时不会调用选择器,但我不知道为什么。几天前他们工作得很好,但出于某种原因现在不行了。我已经检查了 UIView subview 数组,确认它位于顶部。我需要一种方法来查看是否由于某种原因触摸没有到达按钮。我不确定该怎么做。

我希望我能提供更多信息,但这就是我所能提供的。我希望有人能提出一些建议,因为我对此束手无策。

提前致谢!

按钮创建方法:

-(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];
    }
}

最佳答案

我发现其中有几个问题,但我不确定是哪些问题导致它崩溃。首先,您应该在 UIButton 上使用 buttonWithType: 实例化您的按钮:

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

这是因为 UIButton 是一个类簇,你应该让操作系统给你正确的按钮。

其次,您将按钮添加为 UIImageView 的 subview ,默认情况下,userInteractionEnabled 设置为 NO .

This property is inherited from the UIView parent class. This class changes the default value of this property to NO.

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

关于ios - UIButton 突然停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125480/

相关文章:

iphone - MKOverlay View 模糊

ios - 具有不同 ViewController 的 UIPageViewController

ios - iOS AudioUnit播放crack啪声问题(swift)

ios - player-swift库,如何调整播放速率

iOS 9 读取文件权限

iPhone SDK : Tab Character inside a textview

ios - 如何获取多个分段上传任务的上传进度

iphone - 如何在 mfmailcomposerview 中关闭键盘

ios - Xcode - 禁用所有元素的可访问性

objective-c - NSRangeException 超出范围未捕获的异常