2

在我的应用程序中,我的添加联系人页面中有选择器视图。我给出了以下“touchesBegan”方法。但即使我单击要设置的值,它也会关闭选择器。例如,如果我从首选电话选择器中单击“家庭电话”,则选择器将被关闭而不是设置值。只有当我在选择器外部单击时,我才需要关闭选择器。谁能帮我解决这个问题。

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

3 回答 3

6

您可以使用

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

当触摸在选取器视图之外时,这将用于结束编辑

于 2013-07-04T09:39:01.730 回答
2

在您的代码中:

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

 [self dismissViewControllerAnimated:YES completion:nil];

}

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

- (IBAction)buttonClear:(id)sender {

[self dismissViewControllerAnimated:YES completion:nil];

}
于 2013-07-04T09:23:38.077 回答
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];
}

希望对你有帮助

于 2013-07-04T09:34:42.210 回答