2015-08-28 111 views
1

我試圖打開UIPickerView作爲inputView到UITextfield如何將UIPickerView設置爲inputView到iPhone中的UITextField

這是我在textFieldDidBeginEditing代碼:

UIPickerView *categoryPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43, 320, 480)]; 
    categoryPicker.delegate = self; 
    categoryPicker.dataSource = self; 
    [categoryPicker setShowsSelectionIndicator:YES]; 
    txtCategory.inputView = categoryPicker; 

    UIToolbar *_providerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)]; 
    _providerToolbar.barStyle = UIBarStyleBlackOpaque; 
    [_providerToolbar sizeToFit]; 

    NSMutableArray *barItems = [[NSMutableArray alloc] init]; 
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
    [barItems addObject:flexSpace]; 

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissActionSheet)]; 

    [barItems addObject:doneBtn]; 

    [_providerToolbar setItems:barItems animated:YES]; 
    txtCategory.inputAccessoryView = _providerToolbar; 

它的正常工作的第一次,但是當我從UIPickerView選擇任意值,它的隱藏和我UITextfield是編輯和UIPickerView不當時顯示。我究竟做錯了什麼?

+1

我不會把這些代碼在'textFieldDidBeginEditing:',該方法被調用後'UITextField'已經成爲第一個響應者,可能是什麼原因造成您的問題。嘗試將此代碼移至'viewDidLoad'。 –

回答

0

我建議你把你的代碼移動到viewDidLoad:。因爲在textField爲其加載鍵盤時調用textFieldDidBeginEditing:。所以現在不適合改變textField的inputAccessoryView

0
Use this... 

    NSDate *now = [NSDate date]; 

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 

    [dateFormat setDateFormat:@"dd MMM, yyyy"]; 

    UIDatePicker *datePicker = [[UIDatePicker alloc] init]; 

    [datePicker setMaximumDate:now]; 

    datePicker.datePickerMode = UIDatePickerModeDate; 

    txtForDate.text = [dateFormat stringFromDate:datePicker.date]; 

    [datePicker addTarget:self action:@selector(updateTextField:) 
     forControlEvents:UIControlEventValueChanged]; 

    [txtForDate setInputView:datePicker]; 

// txtForDate是的UITextField

相關問題