ios - UIButton不可触摸

标签 ios objective-c xcode5

我有一个 NSDictionary,它保存将在数组中使用的对象集,以根据需要创建多个对象。我的图像右上角有一个删除按钮,但由于某种原因,我无法单击它。我会写出来的...

-(void)addItemsToSomething:(NSMutableArray*)array
{

    for (NSDictionary *item in array)  {

    UIImageView *container....

    UIImageView *boxInContainer....

    UIButton *delete = [UIButton new];
    delete.frame = CGRectMake(70, -4, 24, 26);
    delete.userInteractionEnabled = YES;
    [delete setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
    [delete addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    [boxInContainer addSubview:delete];

    some math....
}
more math
}

-(void)delete:(id)sender
{
    NSLog(@"Do I work");
}

NSLog 永远不会显示在日志中,按钮阴影也不会显示它已被触摸。我什至将 userInteractionEnabled 设置为 YES 只是为了尝试覆盖另一个基地,但按钮仍然盯着我的脸并且不会单击。谢谢您的帮助。

这也全部放在 ScrollView 上,忘记了那部分。

最佳答案

问题是你把它放在UIImageView上。 UIImageViewuserInteractionEnabled 默认设置为 NO。只需更改 boxInContainer.userInteractionEnabled = YES; 按钮就应该可以工作!

哦,如果 boxInContainercontainer 的 subview ,那么您还必须设置 container.userInteractionEnabled = YES;

关于ios - UIButton不可触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24497796/

相关文章:

IOS指纹实现

iPhone 应用程序 : find the Facebook friends who has installed app into their device

iphone - 如何在 NSString 中查找 twitter 句柄

ios - 替代 performSelector :withObject:afterDelay: for animating views consequantially

ios - 迁移到 xcode5 后找不到 stdio.h

ios - 如何在 Xcode 5 上找到 arm-apple-darwin#-llvm-gcc-4.2 编译器?

iphone - 将数组的属性提取到新数组中

ios - 100% 高度按钮/标签/表格单元格 iOS Safari 中的输入

ios - 方法未被调用

objective-c - 在 Objective-C roguelike 中实现多个关卡的最佳方式是什么?