如何在iphone中点击外部时隐藏下拉列表

时间:2013-01-31 10:21:24

标签: iphone objective-c xcode

<以编程方式在iphone中点击外部时如何关闭或隐藏下拉列表?

-(void)RecommendDropDownSelect
{
dropDown=[[DropDownView alloc]initWithArrayData:arr_RecommendName  cellHeight:30 heightTableView:100 paddingTop:-100 paddingLeft:10 paddingRight:20 refView:txt_Recommend animation:BLENDIN openAnimationDuration:0.5 closeAnimationDuration:0.5];
dropDown.delegate=self;
[scr_View addSubview:dropDown.view];
[dropDown openAnimation];


btn_RecommendDropDown.enabled=NO;
}

-(void)dropDownCellSelected:(NSInteger)returnIndex
{
btn_RecommendDropDown.enabled=YES;
txt_Recommend.text=[arr_RecommendName objectAtIndex:returnIndex];

}

3 个答案:

答案 0 :(得分:2)

除了继承UIView并覆盖touchesBegan之外,如果您使用UITapGestureRecognizer

,使用UIViewController似乎更容易

首先,为您的视图设置点按手势:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideDropDown)];
[gestureRecognizer setCancelsTouchesInView:NO];
[self.view addGestureRecognizer:gestureRecognizer];

然后实现隐藏下拉列表的方法:

- (void)hideDropDown
{
    if ((dropDown != nil) && (dropDown.view != nil))
    {
        [drdropDown.view removeFromSuperview];
    }
}

答案 1 :(得分:1)

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
         UITouch *touch = [touches  anyObject];
           if (touch.view==self.view) {
             [dropDown removeFromSuperView];
          }
      }

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches  anyObject];
      if (touch.view==self.view) {
           dropDown.alpha=0;
        }
    }

答案 2 :(得分:0)

试试这个:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
   if(dropDown)
   {
      [drdropDown.view removeFromSuperView];
   }
}
相关问题