我尝试在操作表中添加日期选择器,同时给出另一个按钮
看起来像图像
我可以抽出选择器的时间
但那个小的完成按钮不起作用......
我甚至无法触摸它。
这是我的代码,不是太多......
我无法理解为什么按钮不起作用?
- (IBAction)selectDate:(id)sender{
menu = [[UIActionSheet alloc] initWithTitle:@"Select Date"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[menu setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
startPickerView = [[UIDatePicker alloc] init];
startPickerView.datePickerMode = UIDatePickerModeTime;
[menu sendSubviewToBack:startPickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320,460)];
CGRect pickerRect = startPickerView.bounds;
pickerRect.origin.y = -60;
startPickerView.bounds = pickerRect;
[startPickerView addTarget:self action:@selector(timeChange:) forControlEvents:UIControlEventValueChanged];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventTouchUpInside];
[menu addSubview:startPickerView];
[menu addSubview:closeButton];
[closeButton release];
[menu release];
[startPickerView release];
}
非常感谢任何回复〜
韦伯
答案 0 :(得分:3)
在使用下面的代码之前,我做过类似的事情。在这里,我创建了一个日期选择器视图,并添加到一个带有两个按钮的操作表:工具栏中的“完成”和“取消”。
- (IBAction)selectDate:(id)sender{
UIActionSheet *pickerViewPopup = [[UIActionSheet alloc] init];
const CGFloat toolbarHeight = 44.0f;
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, toolbarHeight, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, toolbarHeight)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar 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(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,self.view.frame.size.width, 464)];
}
使用此处的示例可在以下位置找到:http://www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/
答案 1 :(得分:0)
我认为您使用UIActionSheet的方式是您的问题的根源:UIActionSheet监听工作表上按钮上的事件..以及您使用它的方式,您使用自己的组件覆盖它。
我个人会创建一个自定义的UIView并显示,而不是将DatePicker和Done按钮放在UIActionSheet的顶部。