gpt4 book ai didi

ios - UITextField 上的 UIDatePicker 问题

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

我正在编写一个 TableViewController,其中包含一个名为“信息”的部分,用户在其中输入名称、日期和描述。我的问题是,在我完成日期文本字段上的 NSDate 选择器的所有设置后,当我单击描述的文本字段而不是键盘时,我会看到日期选择器,并将日期存储在日期的文本字段中。

.h 文件

@interface ViewControllerInfo : UITableViewController <UITableViewDelegate, UITextFieldDelegate>{
NSDate *date;
UIActionSheet *dateSheet;
}

@property (weak, nonatomic) IBOutlet UITextField *scadenza;
@property (weak, nonatomic) IBOutlet UITextField *dateTextField;
@property (nonatomic, retain)NSDate *date;

-(void)setDatario;
-(void)dismissDateSet;
-(void)cancelDate;

.m 文件(仅指向所需的函数):

-(void)setDatario;{

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

[dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 44, 0, 0);

UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:pickerFrame];
[datePicker setDatePickerMode:UIDatePickerModeDate];

[dateSheet addSubview:datePicker];

UIToolbar *controlToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];

[controlToolbar setBarStyle:UIBarStyleBlack];
[controlToolbar sizeToFit];

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

UIBarButtonItem *setButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDateSet)];

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

[controlToolbar setItems:[NSArray arrayWithObjects:spacer, setButton, cancelBtn, nil] animated:NO];


[dateSheet addSubview:controlToolbar];

[dateSheet showInView:self.view];

[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];

}

-(void)cancelDate{

[dateSheet dismissWithClickedButtonIndex:0 animated:YES];

}

-(void)dismissDateSet{

NSArray *listofViews = [dateSheet subviews];

for (UIView *subview in listofViews){
if ([subview isKindOfClass:[UIDatePicker class]]){
self.date = [(UIDatePicker*)subview date];

}

[dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];

[dateTextField setText:[dateFormatter stringFromDate:self.date]];

}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[self setDatario];
return NO;
}

我不明白为什么描述的文本字段也很有趣。问题是:为什么描述的文本字段与日期选择器匹配?

最佳答案

像这样替换你的 textFieldShouldBeginEditing 方法

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

if (textField == scadenza) {

return YES;
}
else if(textField == scadenza){

[self setDatario];

return NO;

}
return YES;
}

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

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