iPhone datepicker zamiast klawiatury?

Jak byś to zrobił?

Mam formularz z UITextField. Po kliknięciu w niego chciałbym wyświetlić UIDatePicker zamiast domyślnej klawiatury, która wyskakuje domyślnie? Zauważ, że w moim formularzu są również inne elementy.

Author: xpepermint, 2011-01-24

5 answers

Myślę, że jest to bardziej oficjalny sposób, aby to zrobić:

UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
datePicker.tag = indexPath.row;
textField.inputView = datePicker;
 86
Author: Johan Kool,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-08 06:35:11

W .h

UIDatePicker *datePicker;
NSDate *date;
UIToolbar *keyboardToolbar;

W ViewDidLoad:

if (keyboardToolbar == nil) {
    keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    [keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];

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

    UIBarButtonItem *accept = [[UIBarButtonItem alloc] initWithTitle:@"Accept" style:UIBarButtonItemStyleDone target:self action:@selector(closeKeyboard:)];

    [keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, accept, nil]];
}

self.txtDate.inputAccessoryView = keyboardToolbar;

datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

self.txtDate.inputView = datePicker;


- (void)datePickerValueChanged:(id)sender{

date = datePicker.date;

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/YYYY"];


[self.txtDate setText:[df stringFromDate:date]];
}

W storyboard Dodaj do txtDate - > EditingDidEnd

- (IBAction)establishHour:(id)sender{

hour = hourPicker.date;

NSDateFormatter *hf = [[NSDateFormatter alloc] init];
[hf setDateFormat:@"HH:MM"];

[self.txtHour setText:[hf stringFromDate:hour]];

}

Ustawia klawiaturę jako DatePicker i tworzy pasek narzędzi z przyciskiem akceptującym zaznaczenie.

 8
Author: user1617027,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-02 22:08:50

Przekonwertowałem odpowiedź Johana Koola na swift, jeśli ktoś by chciał. Umieściłem kod w viewDidLoad.

let datePicker = UIDatePicker()
datePicker.datePickerMode = UIDatePickerMode.Date
datePicker.addTarget(self, action: "datePickerValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
textField.inputView = datePicker
 3
Author: Jeremiah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-29 18:48:48
(IBAction)estableceHora:(id)sender{
   Hora = horaPicker.date;
   NSDateFormatter *hf = [[NSDateFormatter alloc] init];
   [hf setDateFormat:@"HH:MM"];
   [self.txtHora setText:[hf stringFromDate:Hora]];
}
 0
Author: sumit sanadhya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-29 11:10:38

Dodano w .pliki h:

UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> 

Dodano w .pliki m:

{

NSDateFormatter *df = [[NSDateFormatter alloc] init];

df.dateStyle = NSDateFormatterMediumStyle;

pTxtDate.text = [NSString stringWithFormat:@"%@",

[df stringFromDate:[NSDate date]]];

NSLog(@"pTxtDate.text %@",pTxtDate.text);

[df release];

[self.view addSubview:pTxtDate]; 

[pTxtDate release];

DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];

DatePicker.datePickerMode = UIDatePickerModeDate;

DatePicker.hidden = NO;

DatePicker.date = [NSDate date];

[DatePicker addTarget:self

  action:@selector(LabelChange:)

forControlEvents:UIControlEventValueChanged];

[self.view addSubview:DatePicker];

    [DatePicker release];

}



- (void)LabelChange:(id)sender {

NSDateFormatter *df = [[NSDateFormatter alloc] init];

df.dateStyle = NSDateFormatterMediumStyle;

pTxtDate.text = [NSString stringWithFormat:@"%@",

[df stringFromDate:DatePicker.date]];

}
 -2
Author: Ela,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-04 06:46:32