Reputation: 69
Iam using the following code for the UIDatePicker, whatever the date iam selecting it is being displayed in the label. But i need to close the Picker after selecting the date. Is this possible please suggest me.
I am using the following code:
- (void)viewDidLoad {
[super viewDidLoad];
//Create label
label = [[UILabel alloc] init];
label.frame = CGRectMake(10, 10, 300, 40);
label.textAlignment = UITextAlignmentCenter;
//Use NSDateFormatter to write out the date in a friendly format
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:[NSDate date]]];
[df release];
[self.view addSubview:label];
[label release];
// Initialization code
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 250)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[datePicker addTarget:self
action:@selector(changeDateInLabel:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[datePicker release];
}
- (void)changeDateInLabel:(id)sender{
//Use NSDateFormatter to write out the date in a friendly format
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datePicker.date]];
[df release];
}
Upvotes: 2
Views: 17865
Reputation: 1778
I recommend you to do something like this:
to use a toolbar with a Done button and maybe a Cancel Button ...
This is just a quick implementation, it may help
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 320, 44)];
textField.text = @"Date";
[self.view addSubview:textField];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *itemDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)];
UIBarButtonItem *itemSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = @[itemSpace,itemDone];
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
datePicker.minimumDate = [NSDate date];
datePicker.date = [NSDate date];
[datePicker addTarget:self action:@selector(didChangePickerDate:) forControlEvents:UIControlEventValueChanged];
textField.inputAccessoryView = toolbar;
textField.inputView = datePicker;
[textField becomeFirstResponder];
Upvotes: 9
Reputation: 552
Following code will work. Tapping on the date label will enable the DatePicker as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
tap.numberOfTapsRequired = 1;
label = [[UILabel alloc] init];
label.frame = CGRectMake(10, 20, 300, 40);
label.userInteractionEnabled = YES;
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor redColor];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:[NSDate date]]];
[label addGestureRecognizer:tap];
[tap addTarget:self action:@selector(datePickerView)];
[self.view addSubview:label];
}
- (void)datePickerView
{
// Initialization code
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 250)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[self.view addSubview:datePicker];
[datePicker addTarget:self
action:@selector(changeDateInLabel:)
forControlEvents:UIControlEventValueChanged];
}
- (void)changeDateInLabel:(id)sender{
//Use NSDateFormatter to write out the date in a friendly format
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datePicker.date]];
}
and using the following method Picker will be resigned from the view on tapping outside the Picker.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[datePicker removeFromSuperview];
}
Upvotes: 0
Reputation: 1174
Maybe better then removing from superview would be setting userInteraction to NO
and you need button for "done", as in where the user is ready to finish chosing date
-(IBAction)buttonDoneClick:(id)sender
{
[datePicker setUserInteractionEnabled:NO];
}
Upvotes: 2
Reputation: 8501
You Have to use button for click on Background to hide the DATEPICKER from your view.
-(IBAction)backGroundClick:(id)sender
{
[datePicker removeFromSuperview];
}
Upvotes: 3