如何解雇ios中的UIPickerView

时间:2013-07-04 09:20:47

标签: iphone ios uipickerview uitouch

在我的应用中,我在添加联系人页面中有选择浏览量。我给出了以下'touchesBegan'方法。但即使我点击要设置的值,它也会取消选择器。例如,如果我从首选电话选择器点击“家庭电话”,则选择器将被解除而不是设置该值。只有当我在选择器外面点击时,我才需要解雇选择器。任何人都可以帮我解决这个问题。

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 [self.AddView endEditing:YES];
}

3 个答案:

答案 0 :(得分:6)

您可以使用

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [touches anyObject];
  if ([touch view] != pickerview)
     [self.AddView endEditing:YES];
}

当触摸位于选择器视图之外时,这将用于结束

答案 1 :(得分:2)

在您的代码中:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

 [self dismissViewControllerAnimated:YES completion:nil];

}

或者您可以在PickerView下放置一个更大的按钮,使其颜色清晰自定义并且全屏尺寸,然后使用(IBAction)内的消除,当您在选择器外部点按时,您的视图将被解除

- (IBAction)buttonClear:(id)sender {

[self dismissViewControllerAnimated:YES completion:nil];

}

答案 2 :(得分:2)

试试这段代码:

UITapGestureRecognizer *single_tap_recognizer;

    single_tap_recognizer = [[[UITapGestureRecognizer alloc]
                              initWithTarget : self.view
                              action         : @selector(upper_button_view_tapped)]
                             autorelease];

    [single_tap_recognizer setNumberOfTouchesRequired : 1];
    [self.view addGestureRecognizer : single_tap_recognizer];

-(void)upper_button_view_tapped
{
 [self .view endEditing:YES];
}

希望对你有所帮助