UIDatePicker不会弹出

时间:2015-09-14 01:45:03

标签: ios objective-c uitextfield uidatepicker

在我的应用中,当用户点击UITextField时,会弹出UIDatePicker。然而,它并没有弹出。当我运行它时,它只显示'选择日期!'在行动表上,没有别的。

这是我的ViewController.m中的代码;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
 {
 [pd resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component

pickerViewDate = [[UIActionSheet alloc] initWithTitle:@"Select the date!"
                                             delegate:self
                                    cancelButtonTitle:nil
                               destructiveButtonTitle:nil
                                    otherButtonTitles:nil];

datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 210, 320, 216)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your specific mode
datePicker.backgroundColor = [UIColor whiteColor];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:@"dd.MM.yyyy"]; //desired format

[datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker

//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleDefault;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
//UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
//[barItems addObject:flexSpace]; // set the left of the bar

[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}

 -(IBAction)dateChanged{

NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[NSLocale alloc]
                        initWithLocaleIdentifier:@"en_US"]];
[FormatDate setDateFormat:@"dd.MM.yyyy"];
pd.text = [FormatDate stringFromDate:[datePicker date]];
}

-(BOOL)closeDatePicker:(id)sender{
[pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
[pd resignFirstResponder];
return YES;
}

-(IBAction)doneButtonClicked{
[self closeDatePicker:self];
}

 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

请知道我做错了什么,我需要帮助。

2 个答案:

答案 0 :(得分:0)

您忘记在UIActionSheet

的末尾显示-(void)textFieldDidBeginEditing:(UITextField *)textField
-(void)textFieldDidBeginEditing:(UITextField *)textField
     {
     [pd resignFirstResponder]; //the textField that you will set the selected date
    datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component

    pickerViewDate = [[UIActionSheet alloc] initWithTitle:@"Select the date!"
                                                 delegate:self
                                        cancelButtonTitle:nil
                                   destructiveButtonTitle:nil
                                        otherButtonTitles:nil];

    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 210, 320, 216)];
    datePicker.datePickerMode = UIDatePickerModeDate; //set your specific mode
    datePicker.backgroundColor = [UIColor whiteColor];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; //or another LocaleIdentifier instead of en_US
    [dateFormatter setDateFormat:@"dd.MM.yyyy"]; //desired format

    [datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker

    //now preparing the toolbar which will be displayed at the top of the datePicker
    pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerToolbar.barStyle=UIBarStyleDefault;
    [pickerToolbar sizeToFit];
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    //UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
    //[barItems addObject:flexSpace]; // set the left of the bar

    [pickerToolbar setItems:barItems animated:YES];
    [pickerViewDate addSubview:pickerToolbar];
    [pickerViewDate addSubview:datePicker];
    [pickerViewDate showInView:self.view];
    [pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
    [pickerViewDate showInView:self.view]; // <---- here, maybe you forgot

}

答案 1 :(得分:0)

阅读Xcode文档中UIActionSheet的说明。它说:

  

UIActionSheet不是设计为子类,也不应该添加   查看其层次结构。

最后一点很重要。您无法在操作表中添加选择器视图,因为您不应该将任何视图添加到操作表。

顺便说一句,UIActionSheet是在iOS 8中,我们即将转向iOS 9,因此截至本周它将被弃用于2个主要版本。因此,你不应该用它进行新的开发。我建议您创建自己的视图控制器并以模态方式呈现它,而不是尝试使用UIActionSheet