自定义UIView不接收触摸事件

时间:2012-06-01 20:35:14

标签: objective-c ios cocoa-touch uiview

我的应用正在阅读博客文章的JSON Feed并显示它们。我已经创建了一个名为PostView的UIView子类,并为它添加了一个nib。显示工作正常,但是,我无法使任何触摸事件起作用。

在笔尖中我有一个UIButton,它的功能应该是执行一个segue(显示给定帖子的注释)。我只是想尝试NSLog,但它不起作用。我尝试在代码中的for循环之后放置按钮,这些操作工作正常,因此我的addTarget方法没有错误。

我还尝试在多个地方将setUserInteractionEnabled设置为YES,也没有帮助。我真的无法想出这个。

有什么建议吗?

更新

for (NSDictionary * post in json) {
    DMBasePostView *nextPost = [DMBasePostView alloc];
    nextPost = [newPost init];
    [nextPost setFrame:CGRectMake(...)];

    // (setting of post-specific stuffs here cut out...)

    // adding target for UIButton:
    [nextPost.showCommentsButton addTarget:self action:@selector(testMethod:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:nextPost];
}

这基本上就是我在ViewController中显示帖子的方式。 PostView具有必要的awakeFromNib方法,因此它可以正确显示笔尖布局。

1 个答案:

答案 0 :(得分:3)

问题在于我在我的控制器中操纵了PostView的高度,但没有添加UIView子视图,因此uibutton以某种方式超出了其超视图的范围。我没有想到它,因为它仍然正确显示,但它没有响应触摸。我不得不修复superview的高度,它解决了我的问题。