简体   繁体   中英

Custom UIView doesn't receive touch events

My app is reading a JSON feed of blog posts and displays them. I've created a subclass of UIView called PostView and hooked a nib to it. Displaying is working fine, however, I can't get any touch events to work.

In the nib I have a UIButton, whose function is supposed to be to perform a segue (to show the comments for the given post). I'm just trying to NSLog something but it's not working. I tried to put buttons after the for loop in code, and those actions work fine, so my addTarget method doesn't have errors.

I have also tried to set setUserInteractionEnabled to YES in multiple places, didnt help either. I really can't figure this one out.

Any suggestions?

update:

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

This is basically how I display posts in my ViewController. PostView has the necessary awakeFromNib method so it displays the nib layout correctly.

The problem was that I manipulated the PostView's height in my controller but not the added UIView subview's, and as a result the uibutton somehow got out of the bounds of its superview. I didn't think of it because it was still displayed correctly, but it didn't respond to touches. I had to fix the superview's height and it solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM