简体   繁体   English

UITextField(s) 上的 UIDatePicker 问题

[英]UIDatePicker issue on UITextField(s)

I am coding a TableViewController with a Section called Information where the user enter a name , a date , a description.我正在编写一个 TableViewController,其中包含一个名为 Information 的部分,用户可以在其中输入名称、日期和描述。 My problem is that after i've done all the settings for the NSDate picker on the date's textfield , when i click on the description's textfield instead of keyboard i see the date picker and it stores date in the date's textfield.我的问题是,当我在日期的文本字段上完成 NSDate 选择器的所有设置后,当我点击描述的文本字段而不是键盘时,我看到日期选择器并将日期存储在日期的文本字段中。

.h file .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 file (pointing only the functions needed): .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;
}

I couldn't understand why is interesting also the description's texfield.我不明白为什么描述的 texfield 也很有趣。 Question is : why the description's textfield is matched with the datepicker ?问题是:为什么描述的文本字段与日期选择器匹配?

Replace your textFieldShouldBeginEditing method like this像这样替换你的 textFieldShouldBeginEditing 方法

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

    if (textField == scadenza) {

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

        [self setDatario];

        return NO;

    }
    return YES;
}

Even better, put a custom UIButton (invisible) of the same size as the textfield right over the textfield.更好的是,在文本字段上放置一个与文本字段大小相同的自定义UIButton (不可见)。 Set the textfield enabled attribute to NO and then show the picker when the button is tapped.将 textfield enabled属性设置为NO ,然后在点击按钮时显示选择器。 When the user selects a date from the picker, update the text in the textfield.当用户从选择器中选择日期时,更新文本字段中的文本。 This way you won't have to manage the textfield's delegate .这样您就不必管理文本字段的delegate I have used both methods and the button is much cleaner and smoother for the user.这两种方法我都用过,按钮对用户来说更干净、更流畅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM