2010-04-14 63 views

回答

2

我做的是实现touchesEnded事件。如果我的UITextField参数发生该事件的话,我隐藏或显示UIPickerView

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{  
    UITouch *touch = [[event allTouches] anyObject]; 

    if (CGRectContainsPoint([self.textField frame], [touch locationInView:self.view])) 
    { 
     //Want to show or hide UIPickerView 
     if(pickerView) 
     { 
      submitButton.hidden = !submitButton.hidden; 
      pickerView.hidden = !pickerView.hidden; 
     } 
    } 
} 
2

这样做将在委托的NSTextField方法最好的地方:

- (BOOL)textShouldBeginEditing:(NSText *)textObject

重写为返回NO,而是唤起选择器视图的方法。返回NO将阻止键盘出现。

+2

我认为这应该是: - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField – Kurt 2011-11-08 17:26:45

+0

你是对的。我得到了错误的API。 – TechZen 2011-11-08 20:31:25

0

http://tmblr.co/ZjkSZteCOUBS

我在我的博客上正好做到这一点奠定了代码和一切。但在下面,我列出了基本概念。

基本上解决方案涉及一个名为ActionSheetPicker的开源项目在github上,并在UITextFieldDelegate上实现textFieldShouldBeginEditing功能。您可以在那里关闭键盘并提供一个UIPickerView。基本的代码如下所示:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
    // We are now showing the UIPickerViewer instead 

    // Close the keypad if it is showing 
    [self.superview endEditing:YES]; 

    // Function to show the picker view 
    [self showPickerViewer :array :pickerTitle]; 

    // Return no so that no cursor is shown in the text box 
    return NO; 
} 

编辑我知道这个问题被问前一段时间,但认为有必要提供一个更近的解决方案。

+0

当您评论时正在更新答案的过程中:) – KVISH 2012-11-19 20:05:40

+0

优秀;谢谢! – 2012-11-19 20:10:15

相关问题