iPhone NewBIe
iPhone NewBIe

Reputation: 407

DatePicker value not changing after even changed

-(IBAction)showDate:(id)sender
{
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 220, 325, 300)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[datePicker setDate:datePicker.date animated:YES];
[self.view addSubview:datePicker];
}



- (void)setDate:(NSDate *)date animated:(BOOL)animated
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *str = [df stringFromDate:date];
NSLog(@"String is------:%@",str);
[dateBtn setTitle:str forState:UIControlStateNormal];
}

i have gone through UIDatePicker Class reference in Apple's Doc.Based on that i have implemented the code as above.there i am getting only current date on button when external method code is written in button action and not changing date if i am change in DatePicker.

if i write code like mention above, i dint get even any title on button. i think, am made blunder mistake, can anyone tell me where the mistake is and explain me in detail.

Thanks alot for help in advance.

Upvotes: 3

Views: 3257

Answers (2)

Anupama
Anupama

Reputation: 373

Try this code:

-(void)timepicker:(UIGestureRecognizer *)date{

    CGRect pickerFrame = CGRectMake(0,200,0,0);

    myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
    [myPicker setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [myPicker addTarget:self action:@selector(pickerChanged:)               forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:myPicker];


}
- (void)pickerChanged:(id)sender{

    NSLog(@"value: %@",[sender date]);

    NSDateFormatter *df = [[NSDateFormatter alloc] init];

    df.dateStyle = NSDateFormatterMediumStyle;

    NSString *date=[NSString stringWithFormat:@"%@",[sender date]];

    NSLog(@"value1: %@",date);

   }

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122391

I think you are getting confused with subclassing, which is something you are not doing here (and don't need to). You need to setup the date picker to call a method of your choosing when the date is changed; something like this:

[datePicker addTarget:self
               action:@selector(dateChanged:)
     forControlEvents:UIControlEventValueChanged];

(See reference).

And then implement the method:

- (void)dateChanged:(UIDatePicker*)datePicker {
    NSLog("Date changed: %@", datePicker.date);
}

Upvotes: 4

Related Questions