我有一个表视图控制器,其中包含一个表示日期的单元格.我跟着这篇文章“
How do I make a modal date picker that only covers half the screen?”,我有一个很大的例外 – 我不能让选择器消失!
我尝试注册事件UIControlEventTouchUpOutside,但似乎这不是由选择器生成的(或者至少在我使用它的模式下).
如何识别用户已完成选择日期?
还有,有没有办法禁止用户直接输入UITextField?我想强迫他们使用选择器.我看到这篇文章“Disable blinking cursor in UITextField?”,但还有另一种方式吗?
Reagards,
– 约翰
解决方法
试试这个代码..在这里我将一个日期选择器放到uitextfield ..它将在右上角的导航栏中有一个完成按钮..所以通过点击完成我将用户可以解雇datepicker ..另一个最好的方法是通过推杆在具有完成按钮的日期选择器上方的工具栏..尝试这将工作..当更改日期选择器时,您可以填充文本字段..希望这有助于..
看到这个stackoverflow链接https://stackoverflow.com/a/4824319/763747这将有完成按钮的datepicker作为键盘上方的工具栏..
#pragma mark -
#pragma mark - TextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return TRUE;
}
- (void) textFieldDidBeginEditing:(UITextField *)textField {
itsRightBarButton.title = @"Done";
itsRightBarButton.style = UIBarButtonItemStyleDone;
itsRightBarButton.target = self;
itsRightBarButton.action = @selector(doneAction:);
if ([textField isEqual:itsIncidentDateTextField])
{
itsDatePicker = [[[UIDatePicker alloc] init] autorelease];
itsDatePicker.datePickerMode = UIDatePickerModeDate;
[itsDatePicker addTarget:self action:@selector(incidentDateValueChanged:) forControlEvents:UIControlEventValueChanged];
//datePicker.tag = indexPath.row;
textField.inputView = itsDatePicker;
}
}
- (IBAction) incidentDateValueChanged:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM d,yyyy"];
itsIncidentDateTextField.text = [dateFormatter stringFromDate:[itsDatePicker date]];
[dateFormatter release];
}