Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get date from datepicker in objective c?

In header file I have:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction)displayDay:(id)sender;
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;

@end

Implementation:

- (IBAction)displayDay:(id)sender {

    NSDate *chosen = [UIDatePicker date];

}

Xcode is telling me I should have UIdatePicker, but when I make that change it says "No know class method for selector 'date'".

Any advice on how to deal with this?

like image 930
sharataka Avatar asked Feb 05 '13 05:02

sharataka


3 Answers

The following code won't work:

NSDate *chosen = [UIDatePicker date];

Because the date is not a class method it's a property of UIDatePicker class.

date

The date displayed by the date picker.

@property(nonatomic, retain) NSDate *date;

Discussion

The default is the date when the UIDatePicker object is created. The date is ignored in the mode UIDatePickerModeCountDownTimer; for that mode, the date picker starts at 0:00. Setting this property does not animate the date picker by spinning the wheels to the new date and time; to do that you must use the setDate:animated: method.

You need to use it like:

NSDate *chosen = [datePicker date];
like image 71
Midhun MP Avatar answered Oct 30 '22 08:10

Midhun MP


Do like this

- (IBAction)displayDay:(id)sender 
{

  NSDate *chosen = [self.datePicker date];

}
like image 40
Anil Varghese Avatar answered Oct 30 '22 08:10

Anil Varghese


- (IBAction)displayDay:(id)sender {

    NSDate *chosen = [yourdatepicker date];
      NSLog(@"%@",chosen);
}

try like this.

like image 34
Balu Avatar answered Oct 30 '22 07:10

Balu