私は iOS 開発が初めてで、日付ピッカーから日付を取得する 2 つのテキスト フィールドを設定する方法を知る必要があります。コードは次のとおりです。
.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
もう 1 つのテキスト フィールドでは、datepicker の代わりにプレーン キーボードが表示されます。これを達成する方法を知っている人はいますか?