2010-04-14 54 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

相關問題