简体   繁体   English

在外面点击时从另一个子视图中删除一个子视图

[英]removing a subview from another subview when tapped outside

i am trying to build my first iphone app, please do help me out of this issue!! 我正在尝试构建我的第一个iPhone应用程序,请帮我解决这个问题!! 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;
        }
    }
}

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. 您将这段代码放在ViewController.m中,以便在您单击self.view时将其激活。您的scrollView会覆盖self.view,因此该操作无效。

Subclass your scrollView and select type in your IB, inside YourScrollView.m, add this code: 子类化您的scrollView并在YourScrollView.m中的IB中选择类型,添加以下代码:

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

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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