У вас могут быть поля для начальной и конечной даты, и когда пользователь нажимает на одно из полей, отображается детальный вид с помощью средства выбора. Проверьте, как вы вводите дату и время в приложении «Календарь».
Я также недавно видел какое-то приложение, в котором средство выбора даты показывалось как модальное представление (прокручивалось снизу, как клавиатура). Тогда он занимает только экран, когда пользователь фактически выбирает дату, и исчезает, когда она закончит.
Обновление : ОК, в данный момент у меня нет доступа к Mac, и я набрал все это в блокноте, так что он, вероятно, не будет работать без изменений:
Сначала я бы создал пользовательский UIViewController:
@interface MyDatePickerViewController : UIViewController <UIPickerViewDelegate> {
UITextfield *textfieldBeingEdited;
UIDatePicker *datePicker;
}
@property (nonatomic, assign) UITextfield *textfieldBeingEdited; // not sure about 'assign'
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
- (IBAction)dismiss:(id)sender;
@end
@implementation MyDatePickerViewController
@synthesize textfieldBeingEdited;
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
if (self = [super initWithNibName:nibName bundle:nibBundle]) {
// do other initializations, if necessary
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// TODO: get the date from textfieldBeingEdited and set it in the UIDatePicker
// You'd want the correct date preset when the view is animated in
self.datePicker.date = /* the date */;
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
NSDate *date = self.datePicker.date;
// TODO: format the date
NSString *formattedDate = ...;
// update the text field
textfieldBeingEdited.text = formattedDate;
}
- (IBAction)dismiss:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
@end
Когда пользователь нажимает на одно из ваших полей даты, вы создаете UIViewController и отображаете его как модальное представление:
UIViewController *datePickerViewController = [[MyDatePickerViewController alloc]
initWithNibName:@"nib name goes here" bundle:nil];
datePickerViewController.textfieldBeingEdited = /*the field with the start date or end date*/;
[self presentModalViewController:datePickerViewController animated:YES];
[datePickerViewController release];
Некоторые комментарии о файле пера:
- имя класса владельца файла будет
быть MyDatePickerViewController
- добавить UIDatePicker, подключить его к выходу datePicker, установить его делегата и т. Д. Для владельца файла
- добавить кнопку, чтобы пользователь мог закрыть представление и подключить его к выходу -dismiss: в MyDatePickerViewController
Обновление 2 : чтобы предотвратить отображение клавиатуры, я сделал свой контроллер представления делегатом для UITextField. Затем я представляю контроллер модального представления в -textFieldShouldBeginEditing: и возвращаю NO. Возврат NO останавливает отображение клавиатуры:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
MyDatePickerViewController *modal = [[MyDatePickerViewController alloc] initWithNibName:nil bundle:nil];
modal.textfieldBeingEdited = self.dateField;
[self presentModalViewController:modal animated:YES];
[modal release];
return NO;
}