我是 ios 开发的新手,需要知道如何设置两个从日期选择器获取日期的文本字段。这是代码:
.h 文件
#import <UIKit/UIKit.h>
@interface DatePickerForTermins : UIViewController<UITextFieldDelegate>
{
//NSDate *frmDate;
UIActionSheet *dateSheet;
UIActionSheet *dateSheet2;
}
@property (weak, nonatomic) IBOutlet UITextField *fromDateTextField;
@property (weak, nonatomic) IBOutlet UITextField *toDateTextField;
@property (weak, nonatomic) IBOutlet UILabel *chosenFromDateLabel;
@property (weak, nonatomic) IBOutlet UILabel *chosenToDateLabel;
@property (weak, nonatomic) NSDate *frmDate;
@property (weak, nonatomic) NSDate *toDate;
-(void)setDateFRM;
-(void)dismissDateSet;
-(void)cancelDateSet;
-(void)setDateTO;
-(void)dismissDateSet2;
-(void)cancelDateSet2;
@end
和 .m 文件:
#import "DatePickerForTermins.h"
@interface DatePickerForTermins ()
@end
@implementation DatePickerForTermins
@synthesize fromDateTextField;
@synthesize chosenFromDateLabel;
@synthesize frmDate;
@synthesize chosenToDateLabel;
@synthesize toDateTextField;
@synthesize toDate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setDateFRM
{
dateSheet=[[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleDefault];
CGRect pickerFrame=CGRectMake(0, 44, 0, 0);
UIDatePicker *frmDatePicker=[[UIDatePicker alloc]initWithFrame:pickerFrame];
[frmDatePicker setDatePickerMode:UIDatePickerModeDate];
[dateSheet addSubview:frmDatePicker];
UIToolbar *controlToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleDefault];
[controlToolBar sizeToFit];
UIBarButtonItem *spacer=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton=[[UIBarButtonItem alloc]initWithTitle:@"Set Date" style:UIBarButtonItemStyleDone target:self action:@selector(dismissDateSet)];
UIBarButtonItem *cancelButton=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelDateSet)];
[controlToolBar setItems:[NSArray arrayWithObjects:spacer,cancelButton,setButton, nil]animated:NO];
[dateSheet addSubview:controlToolBar];
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0, 0, 320, 425)];
}
-(void)setDateTO
{
dateSheet2=[[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet2 setActionSheetStyle:UIActionSheetStyleDefault];
CGRect pickerFrame=CGRectMake(0, 44, 0, 0);
UIDatePicker *frmDatePicker=[[UIDatePicker alloc]initWithFrame:pickerFrame];
[frmDatePicker setDatePickerMode:UIDatePickerModeDate];
[dateSheet2 addSubview:frmDatePicker];
UIToolbar *controlToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, dateSheet2.bounds.size.width, 44)];
[controlToolBar setBarStyle:UIBarStyleDefault];
[controlToolBar sizeToFit];
UIBarButtonItem *spacer=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton=[[UIBarButtonItem alloc]initWithTitle:@"Set Date" style:UIBarButtonItemStyleDone target:self action:@selector(dismissDateSet2)];
UIBarButtonItem *cancelButton=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelDateSet2)];
[controlToolBar setItems:[NSArray arrayWithObjects:spacer,cancelButton,setButton, nil]animated:NO];
[dateSheet2 addSubview:controlToolBar];
[dateSheet2 showInView:self.view];
[dateSheet2 setBounds:CGRectMake(0, 0, 320, 425)];
}
-(void)cancelDateSet
{
[dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancelDateSet2
{
[dateSheet2 dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)dismissDateSet
{
NSArray *listOfSubviews=[dateSheet subviews];
for (NSString *sre in listOfSubviews) {
NSLog(@"Lista Sub:%@",sre);
}
for (UIView *subView in listOfSubviews) {
if([subView isKindOfClass:[UIDatePicker class]]){
self.frmDate=[(UIDatePicker*)subView date];
}
}
NSDateFormatter *dateFormater=[[NSDateFormatter alloc]init];
[dateFormater setDateFormat:@"dd/MM/yyyy"];
[fromDateTextField setText:[dateFormater stringFromDate:self.frmDate]];
// NSCalendar *calendar=[NSCalendar currentCalendar];
// unsigned unitFlags= NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
// NSDateComponents
NSString *outputStr=[NSString stringWithFormat:@"From Datum je:%@",self.frmDate];
[self.chosenFromDateLabel setText:outputStr];
[dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)dismissDateSet2
{
NSArray *listOfSubviews=[dateSheet2 subviews];
for (NSString *sre in listOfSubviews) {
NSLog(@"Lista Sub2:%@",sre);
}
for (UIView *subView in listOfSubviews) {
if([subView isKindOfClass:[UIDatePicker class]]){
self.toDate=[(UIDatePicker*)subView date];
}
}
NSDateFormatter *dateFormater=[[NSDateFormatter alloc]init];
[dateFormater setDateFormat:@"dd/MM/yyyy"];
[toDateTextField setText:[dateFormater stringFromDate:self.toDate]];
// NSCalendar *calendar=[NSCalendar currentCalendar];
// unsigned unitFlags= NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
// NSDateComponents
NSString *outputStr=[NSString stringWithFormat:@"From Datum je:%@",self.frmDate];
[self.chosenToDateLabel setText:outputStr];
[dateSheet2 dismissWithClickedButtonIndex:0 animated:YES];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self setDateFRM];
return NO;
}
@end
另一个文本字段只提供普通键盘而不是日期选择器。有谁知道如何做到这一点?