gpt4 book ai didi

iPhone 在 UITextField touch 上显示日期选择器

转载 作者:行者123 更新时间:2023-12-03 18:17:26 25 4
gpt4 key购买 nike

我关注了这个帖子:datepicker by clicking on textfield

我导入了以下两个协议(protocol):

@interface ViewController : UIViewController<UIActionSheetDelegate, UITextFieldDelegate> {

然后,在实现中,我使用以下内容:

- (void)viewDidLoad {

textField.delegate = self;

[super viewDidLoad] }

最后,我放置了实际代码来显示日期选择器(来自线程)。我还在 IB 中将其全部链接起来。

    //Date Picker
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{
[aTextField resignFirstResponder];

pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];

UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];

NSMutableArray *barItems = [[NSMutableArray alloc] init];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
[barItems addObject:doneBtn];

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];

[pickerToolbar setItems:barItems animated:YES];

[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}

-(void)doneButtonPressed:(id)sender{
//Do something here here with the value selected using [pickerView date] to get that value
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}

-(void)cancelButtonPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}

但是,当我单击 UITextField 时,我得到:

enter image description here

我做错了什么?

最佳答案

您可以使用UITextField 输入 View 属性。只需在代码中分配初始化一个常规的 UIDatePicker 并将其分配给文本字段的 inputView 即可。执行此操作后,将显示 UIPickerView 而不是键盘。如果您需要任何代码,请随时询问:)

编辑:代码

您首先将 textField 与 IB 连接起来

@interface myViewController <UITextFieldDelegate>
@property (nonatomic,weak) IBOutlet UITextField *myTextField;
@end

实现中

UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
if (@available(iOS 13.4, *)) {
[datePicker setPreferredDatePickerStyle:UIDatePickerStyleWheels];
}
[self.myTextField setInputView:datePicker];

当您点击 textField 时,将会弹出一个 UIDatePicker。请注意我添加到 datePicker 的操作。

[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];

要在用户每次在选择器中滚动时更新 textField,您只需在 viewController 中的某处实现 updateTextField 方法

-(void)updateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)self.myTextField.inputView;
self.myTextField.text = [NSString stringWithFormat:@"%@",picker.date];
}

使用最后一个方法,每次用户旋转选择器时,文本字段都会更新!

  • 您可以使用 NSDateFormatter 来改进字符串的输出。

关于iPhone 在 UITextField touch 上显示日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11197855/

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