2012-09-01 27 views
1

我有UITableView上的UITextField,我正在使用数字键盘,但是我希望它在用户单击除UiTextField之外的任何其他选项时被解除。最优雅的方式来解除UiTableView上的UITextField的数字键盘

我已经看到了几种解决方案,但似乎没有一个确定的答案。例如有的谈的手势,当我执行他们,他们不会出现使用下面的代码工作:

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

    [[self view] endEditing:TRUE]; 

} 

正如你可以看到,我想,但没有,这似乎是工作的一种方式。有人可以引导我吗?

感谢

+1

关键是要送'resignFirstResponder'取其控制是目前第一个响应者帮助(即你的UITextField)。 –

回答

3

通常情况下,你会想打测试反对该不该取消键盘区域的触摸;但总的来说,要求是告诉当前的对焦控制器将其状态“退出”为“firstResponder”。这可能是这样的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
[self.userInput resignFirstResponder]; 
} 

但是,您可能还需要考虑这个特殊的手势识别,让你不必过度分析的NSSet接触的,从长远来看(代表对GestureRecognizer确定实际的“解雇认罪!”水龙头和一个“我可以滚动吗?”刷卡之差的任务。

+0

当我使用这段代码时没有任何反应。 – jini

+0

我相信UiTableViewController不接受触摸:S – jini

3

您应该使用resignFirstResponder代替:

[textField resignFirstResponder]; 
2

使用下面的代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    // Note the '!': 
    if(![[touch view] class] isKindOfClass [UITableViewController class]]){ 
    // It's not a bubble they touched, dismiss the keyboard: 
    [self.view endEditing:YES]; 
    } 
    [super touchesBegan:touches withEvent:event]; 
} 

否则

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    // Note the '!': 
    if(![[touch view] class] isKindOfClass [UITableViewController class]]){ 
    // It's not a bubble they touched, dismiss the keyboard: 
    [textField resignFirstResponder]; 

    } 
    [super touchesBegan:touches withEvent:event]; 
} 

这个做你想要什么