2014-04-10 47 views
0

當您單擊dateTextField,它顯示datePickertoolBar
我添加doneButtontoolBar,但doneButton無法點擊。
我該如何解決?的UIBarButtonItem上的UIDatePicker作爲inputView不能點擊

@property (nonatomic, retain) UIDatePicker* datePicker; 
    @property (nonatomic, retain) UIToolBar* toolBar; 
    @property (nonatomic, retain) UITextField* dateTextField; 

    self.datePicker = [[UIDatePicker alloc]init]; 
    self.datePicker.datePickerMode = UIDatePickerModeTime; 
    self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, -40, 320, 40)]; 
    [self.datePicker addSubview:self.toolBar]; 
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closePicker)]; 
    NSArray* barItems = [NSArray arrayWithObjects:doneButton, nil]; 
    [self.toolBar setItems:barItems animated:YES]; 

    self.dateTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 5, 100, 25)]; 
    self.dateTextField.delegate = self; 
    self.dateTextField.inputView = self.datePicker; 
    [self.view addSubview:self.dateTextField]; 

謝謝。

回答

0

不要添加工具欄爲您的日期選擇器的子視圖。相反,將您dateTextField的inputAccessoryView屬性工具欄(與工具欄正常幀)。事情是這樣的:

@property (nonatomic, retain) UIDatePicker* datePicker; 
@property (nonatomic, retain) UIToolBar* toolBar; 
@property (nonatomic, retain) UITextField* dateTextField; 

self.datePicker = [[UIDatePicker alloc]init]; 
self.datePicker.datePickerMode = UIDatePickerModeTime; 


self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; 
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closePicker)]; 
NSArray* barItems = [NSArray arrayWithObjects:doneButton, nil]; 
[self.toolBar setItems:barItems animated:YES]; 

self.dateTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 5, 100, 25)]; 
self.dateTextField.delegate = self; 
self.dateTextField.inputView = self.datePicker; 
self.dateTextField.inputAccessoryView = self.toolbar; 
[self.view addSubview:self.dateTextField]; 

還檢查了這個蘋果本文的詳細信息:

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html