0

I have this code which adds a UIDatepicker (datePicker) when the UITextField (dateDue) is tapped:

In the viewDidLoad:

// Initiate datepicker and assign to due date
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[datePicker addTarget:self action:@selector(dateChanged:)
 forControlEvents:UIControlEventValueChanged];
_dueDate.inputView = datePicker;

It works great, but I can't seem to get the date value in the changedDate function, it keeps returning null:

- (void) dateChanged:(id)sender{
    // Add the date to the dueDate text field
    NSLog(@"Date changed: %@", datePicker.date);
}

Does anyone have any idea why this is happening?

Peter

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Peter Stuart
  • 2,362
  • 7
  • 42
  • 73

1 Answers1

1

In your dateChanged: method you are accessing a variable named datePicker. Is this an instance variable?

Assuming it is, you never set it. In your viewDidLoad you have a local variable named datePicker that you use but that is different from the instance variable with the same name.

In viewDidLoad, change:

UIDatePicker *datePicker = [[UIDatePicker alloc] init];

to:

datePicker = [[UIDatePicker alloc] init];

That will fix it.

You should also change your dateChanged: method to:

- (void) dateChanged:(UIDatePicker *)picker {
    // Add the date to the dueDate text field
    NSLog(@"Date changed: %@", picker.date);
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • WHen I change the code I get errors: "Use of undeclared identifier 'datePicker'; did you mean '_datePicker'?" – Peter Stuart Feb 22 '14 at 16:45
  • Do you have a property for your date picker? If so, use `self.datePicker`. – rmaddy Feb 22 '14 at 16:46
  • In the .h file? No I don't, because it is created programatically – Peter Stuart Feb 22 '14 at 16:47
  • Then what `datePicker` variable was being accessed in your `dateChanged:` method? – rmaddy Feb 22 '14 at 16:48
  • Not sure, but I synthesized the datPicker var and that seemed to do it. I thought by having the code "UIDatePicker *datePicker = [[UIDatePicker alloc] init];" that would make the datePicker var available everywhere. – Peter Stuart Feb 22 '14 at 16:49
  • No, that just creates a local variable. It will only be available with the curly braces it is declared. You need an instance variable (either an explicit instance variable or a property). – rmaddy Feb 22 '14 at 16:50
  • I understand, I didn't consider the variable scope. Can I create a property in the h file, even if the UI element is created in the viewDidLoad. Does adding "@synthesize datePicker = datePicker;" make it a global var and is it safe? – Peter Stuart Feb 22 '14 at 16:52
  • Do not put anything in the .h. What you want is something private, not public. Whether you use a private property or a private instance variable is up to you. But which ever you choose should be declared in the .m file. And if you do use a property, do not bother with the `@synthesize` line. That hasn't been needed for quite some time now. – rmaddy Feb 22 '14 at 16:54
  • When I remove it, it stops working? How do I make datePicker a property instead? – Peter Stuart Feb 22 '14 at 16:56
  • Remove what from where? Did you declare the instance variable in the .m file? Did you add the ivar to the `@implementation { }` block? – rmaddy Feb 22 '14 at 16:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48148/discussion-between-peter-stuart-and-rmaddy) – Peter Stuart Feb 22 '14 at 16:58
  • See http://stackoverflow.com/questions/13263229/objective-c-instance-variables-newbie/13263345#13263345 – rmaddy Feb 22 '14 at 16:59