简体   繁体   中英

iOS: Removing UIView that's on top of UITableView leads to inability to interact with table

I have a UITableView with a bunch of rows. When a user taps on a row, a custom pop-up (which is a custom UIView) will appear on top of the table:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PopUp *myPopUp = [[PopUp alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    [self.view addSubview:myPopUp];
}

I'm loading my custom UIView PopUp from a nib:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadNib];
    }
    return self;
}

- (void) loadNib
{
    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"PopUp" owner:self options:nil];
    UIView *mainView = [subviewArray objectAtIndex:0];
    [self addSubview:mainView];
}

In the PopUp, there is a button when pressed that causes the PopUp to close:

- (IBAction)closePopUp:(id)sender
{
    [self removeFromSuperview];
} 

The PopUp disappears when the button is pressed. However, the UITableView underneath cannot be interacted with anymore (ie the user cannot scroll the table, cannot tap on another row, etc.). I would like the PopUp to disappear and have the table be fully interactive again. Can anyone explain why this is happening and how I may fix this? Thanks!


Edited with screenshots

  1. UITableView with a row of data: http://imgur.com/PlIufHI,xGKxUul,qkt27oZ#0

  2. When a row is selected, myPopUp appears on top: http://imgur.com/PlIufHI,xGKxUul,qkt27oZ#1

  3. When the "x" custom button is pressed, it calls closePopUp, which removes myPopUp from the superview: http://imgur.com/PlIufHI,xGKxUul,qkt27oZ#2

  4. User is unable to interact with the table now. User cannot select a row, scroll through the table, etc.

I don't know what's going on in your specific case, but I can tell you that adding subviews to a UITableView may lead to unexpected behavior like this and it's generally a bad idea.

In order to fix what's happening and get a cleaner structure, I would suggest you to add the popup view to the window, rather than to the table view.

[self.view.window addSubview:myPopUp];

You are actually removing the view that you loaded from the nib file, but the parent is another blank UIView that is capturing every touch within the (0, 0, 320, 568) rect.

Try removing the superview from the closePopUp method:

[self.superview removeFromSuperview];

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