gpt4 book ai didi

ios - UIDatePicker 不弹出

转载 作者:行者123 更新时间:2023-12-01 16:29:35 28 4
gpt4 key购买 nike

在我的应用程序中,当用户单击 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

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

最佳答案

阅读 UIActionSheet 的描述在 Xcode 文档中。它说:

UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy.



最后一点很重要。您不能将选择器 View 添加到操作表,因为您不应该添加 任意 操作表的 View 。

顺便说一句, UIActionSheet它是在 iOS 8 中,我们即将迁移到 iOS 9,因此从本周开始,它将在 2 个主要版本中被弃用。因此,您不应该使用它进行新的开发。我建议您创建自己的 View Controller 并以模态方式呈现它,而不是尝试使用 UIActionSheet .

关于ios - UIDatePicker 不弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32556271/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com