当触摸tableview外。如何解雇tableView?

时间:2016-03-12 07:10:42

标签: ios objective-c uitableview uiview touch-event

当我们点击视图时如何关闭tableview?我正在使用触摸开始方法。但它不起作用?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touches began");
UITouch *touch = [touches anyObject];   
if(touch.view!=myTableView){
   myTableview.hidden = YES;
}
}

当我们点击view时,如何禁用该表。我有三个表视图?

2 个答案:

答案 0 :(得分:0)

试试这个

<强> viewDidLoad中

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelGesture:)];
        [self.view addGestureRecognizer:tap];

<强> ActionMethod

- (void) handelGesture:(UITapGestureRecognizer*)sender
    {
        myTableview.hidden = YES;
    }

希望这有帮助。

答案 1 :(得分:-1)

如果您修改了要求,请检查此代码是否有效谢谢

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
         NSArray *subviews = [self.view subviews];
        for (id objects in subviews) {
            if ([objects isKindOfClass:[UITextField class]]) {
                UITextField *theTextField = objects;
                if ([objects isFirstResponder]) {
                    [theTextField resignFirstResponder];
                }
            } 
        }
    }