我在视图控制器类中创建自定义datepicker。然后我需要在另一个视图控制器类中的datepicker。所以我想创建任何视图控制器类可以使用并显示日期的常见日期选择器。那我该怎么用呢请分享代码。
我使用此代码:
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Date" delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setBackgroundColor:[UIColor grayColor ]];
[actionSheet setFrame:CGRectMake(0, 470, 375, 200)];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake ( 0, 10, 300, 100)];
dateFormatter = [[NSDateFormatter alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
[datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:datePicker];
UIToolbar *toolbarPicker = [[UIToolbar alloc] init];
toolbarPicker.backgroundColor=[UIColor grayColor];
// toolbarPicker.barStyle=UIBarStyleBlackOpaque;
[toolbarPicker sizeToFit];
NSMutableArray *itemsBar = [[NSMutableArray alloc] init];
//calls DoneClicked
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClicked)];
[itemsBar addObject:btnDone];
[toolbarPicker setItems:itemsBar animated:YES];
[actionSheet addSubview:toolbarPicker];
[self.view addSubview:actionSheet];
}
-(IBAction)dateChanged{
self.dateEntry.text = [dateFormatter stringFromDate:[datePicker date]];
}
-(IBAction)doneClicked{
[actionSheet removeFromSuperview];
self.dateEntry.userInteractionEnabled = YES;
}
答案 0 :(得分:1)
创建UIView/NSObject
的自定义类,在那里添加逻辑&然后实现委托设计模式来管理它。
如果您需要进一步澄清,请告诉我。