2011-06-07 25 views
7

我想要一個UIPickerView顯示當我按下按鈕(就像鍵盤),然後當用戶點擊屏幕上的任何地方時離開。我怎樣才能做到這一點?謝謝。如何像鍵盤一樣呈現選取器視圖?

更多的背景:我有一個UITextField在UITableViewCell中稱爲月。現在對我來說最好的選擇是在那裏放置一個UIPikcerView,用戶只需選擇月份而不必輸入它(這是更好的方法),這樣會更容易。但UIPickerView在UITableViewCell中看起來非常難看,更不用說我無法改變它的巨大尺寸。那麼在這種情況下我應該怎麼做?

+0

您的標題不是很具描述性。請提供更多關於你想要達到什麼的內容(標題中) – Julian 2011-06-07 17:34:31

+0

對不起。標題由其他人更新,但我現在也爲這個問題提供了更多背景。 – sumderungHAY 2011-06-07 17:51:03

回答

13

這就是你應該如何使用UIPickerView在UITableView中的textField。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    cell.selectionStyle= UITableViewCellSelectionStyleNone; 
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)]; 
    textField.clearsOnBeginEditing = NO; 
    textField.textAlignment = UITextAlignmentRight; 
    textField.delegate = self; 
    [cell.contentView addSubview:textField]; 
    //textField.returnKeyType = UIReturnKeyDone; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 

     // if you want a UIPickerView for first row... then do the following // 
     // Here.. packSizePickerView is an object of UIPickerView 

    if (indexPath.row == 1) 
    { 
    textField.tag=0; 
    textField.delegate= self; 
    packSizePickerView.delegate = self; 
    packSizePickerView = [[UIPickerView alloc] init]; 
    packSizePickerView.autoresizingMask=UIViewAutoresizingFlexibleWidth; 
    packSizePickerView.showsSelectionIndicator=YES; 
    textField.inputView = packSizePickerView; 
    } 

    // if you want to select date/months in row 2, go for UIDatePickerView // 

    if (indexPath.row == 1) 
    { 
     textField.tag = 1; 
     UIDatePicker *datePicker = [[UIDatePicker alloc] init]; 
     datePicker.datePickerMode = UIDatePickerModeDate; 
     [datePicker addTarget:self action:@selector(datePickerValueChangedd:) forControlEvents:UIControlEventValueChanged]; 
     datePicker.tag = indexPath.row; 
     textField.delegate= self; 
     textField.inputView = datePicker; 
     [datePicker release]; 

    } 

} 

// Configure the cell... 

NSInteger row = [indexPath row]; 
cell.textLabel.text = [myArray objectAtIndex:row]; 

return cell; 

}

而對於datePickerValueChangedd

- (void)datePickerValueChangedd:(UIDatePicker*) datePicker{ 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)]; 
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
df.dateStyle = NSDateFormatterMediumStyle; 
label.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]]; 

    NSLog(@"I am inside the datePickerValueChanged - - %@", label.text); 
[df release]; 
    globalValue1 = label.text; 

    // use a global variable to copy the value from the pickerView. // 
    }  
在textFieldDidEndEditing

現在:

-(void) textFieldDidEndEditing:(UITextField *)textField { 
if (textField.tag ==0) 
    [textField setText: globalValue0]; 

if (textField.tag ==1) 
    [textField setText: globalValue1]; 

} 

而辭職的UIDatePicker,設置了UIView作爲UIControl和

resignFirstResponder 
+0

很好的回答!這正是我正在尋找的! +1 – Garoal 2012-08-02 12:11:33

0

我認爲你需要把你的UIPickerView放在可視屏幕的「外部」,並在按下按鈕時爲其設置動畫效果。我不太確定擺脫它的最好方法是什麼,但是當拾取器可見時,您可能會聽取父視圖上的點按操作,然後將其重新制作回去。

相關問題