UIBarbutton操作不适用于UIDatePicker

时间:2013-11-21 12:55:19

标签: ios iphone

我正在使用Date选择器并且运行良好,但是datepicker工具栏中DONE按钮上的操作无法正常工作。 请帮忙解决这个问题。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~

 - (IBAction)timepicker:(id)sender {
     timepicker.hidden = NO;

     NSDateFormatter *df = [[NSDateFormatter alloc] init];
     df.timeStyle = NSDateFormatterShortStyle;
     timebtn.titleLabel.text = [NSString stringWithFormat:@"%@",[df stringFromDate:timepicker.date]];
     UIToolbar *toolbar = [[UIToolbar alloc] init];
     toolbar.barStyle = UIBarStyleBlackOpaque;
     toolbar.frame=CGRectMake(0,4,timepicker.frame.size.width,40);

     UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

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

     [toolbar setItems:[NSArray arrayWithObject:doneBtn] animated:NO];

     [timepicker addSubview:toolbar];

 }

 - (IBAction)pickerDoneClicked:(id)sender
 {
     NSLog(@"DONE IS PRESSED");
     [timepicker setHidden:YES];
 }

3 个答案:

答案 0 :(得分:0)

使用此方法代替 - (IBAction)pickerDoneClicked:(id)sender

- (void)pickerDoneClicked:(id)sender
 {
     NSLog(@"DONE IS PRESSED");
     [timepicker setHidden:YES];
 }

答案 1 :(得分:0)

IBAction是Interface Builder Action的缩写,它是在界面构建器(xib和storyboards编辑器)中定义的控件事件上调用的。虽然IBAction解析为无效,但如果您已在代码中定义了目标和操作,则控制器将无法识别它。

所以,使用void返回类型,正如Ramdy所说。

- (void)pickerDoneClicked:(id)sender
 {
     NSLog(@"DONE IS PRESSED");
     [timepicker setHidden:YES];
 }

答案 2 :(得分:0)

尝试将UIToolbarUIDatePicker(具有适当的帧大小)添加到UIView,然后再将其添加到子视图中。

我遇到了同样的问题,结果是选择器视图与工具栏重叠,导致UIDatePicker劫持了连接到UIBarButtonItem的选择器。

// picker with the appropriate x,y,w,h (216 is default height of UIDatePicker)
timepicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, 216)];
timepicker.datePickerMode = UIDatePickerModeTime;
[timepicker addTarget:self action:@selector(timePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

UIBarButtonItem *timePickerDoneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(timePickerDoneTapped:)];
UIBarButtonItem *timePickerFlex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
// toolbar with appropriate x,y,w,h
UIToolbar *timePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[timePickerToolbar setBarStyle:UIBarStyleDefault];
[timePickerToolbar sizeToFit];
[timePickerToolbar setItems:@[timePickerFlex,timePickerDoneButton] animated:YES];

// view with appropriate x,y,w,h
UIView *timePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 260)];
[timePickerView addSubview:timePickerToolbar];
[timePickerView addSubview:timepicker];