2

绘制的框架UISearchBar如下:

UISearchBar *sBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 280, 40)];

当用户点击任何区域而不是上面定义的区域时,我只想关闭键盘。

是否有任何UISearchBar委托方法可以这样做,或者简单的方法。提前谢谢。

4

1 回答 1

1

您需要处理窗口中其他视图的点击/触摸事件,并调用:

if (sBar.isFirstResponder)
    [sBar resignFirstResponder];

sBar 需要是你的 UIViewController 的一个属性

要处理触摸事件,请将这些添加到您的 UIViewController:

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

    - (void) touchesMoved:(NESet *)touches withEvent:(UIEvent *)event
    {
    }

    - (void) touchesEnded:(NESet *)touches withEvent:(UIEvent *)event
    {
    }

- (void) touchesCancelled:(NESet *)touches withEvent:(UIEvent *)event
    {
    }

并至少从 touchesBegan 方法调用 [sBar resignFirstResponder]。

于 2012-09-05T16:56:09.320 回答