Question

i am trying to build my first iphone app, please do help me out of this issue!! i have my scrollview as a subview of my controller's view and some controls like label, button, textfield and tableview has been placed as subviews to this scrollview. now when i tap the button, my table view becomes visible but i could'nt dismiss this tableview when tapped outside the tableview(i mean when tapped on the scrollview).

below is my code snippet thru' which i tried to dismiss my tableview, pls help me out!

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *aTouch = [touches anyObject];
    if (aTouch.tapCount == 1)
    {
        CGPoint p = [aTouch locationInView:self.scrollview];
        if (!CGRectContainsPoint(myTableView.frame, p))
        {
            myTableView.hidden = YES;
        }
    }
}
Was it helpful?

Solution

You put this code inside ViewController.m, so that it just active when you tap on self.view.Your scrollView overlay your self.view so that action not active.

Subclass your scrollView and select type in your IB, inside YourScrollView.m, add this code:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *aTouch = [touches anyObject];
    if (aTouch.tapCount == 1)
    {
        CGPoint p = [aTouch locationInView:self];
        for (UIView *aView in self.subviews) {
            if (([aView isKindOfClass:[UITableView class]])&&(!CGRectContainsPoint(aView.frame, p)))
            {
                [aView setHidden:YES];
            }

        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top