UIDatePut上的UIBarButtonItem作为inputView不可单击

时间:2014-04-10 17:12:41

标签: ios objective-c uitoolbar uidatepicker

当您点击dateTextField时,它会显示datePicker toolBar 我将doneButton添加到toolBar,但doneButton无法点击 我该如何解决?

    @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];

谢谢。

1 个答案:

答案 0 :(得分: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];

另请查看此Apple文章了解更多信息:

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