I am using uidatepicker and throwing exception when I try to get the date from date picker.
.h file:
#import <UIKit/UIKit.h>
@protocol TimeDelegate <NSObject>
- (void)returnTime:(NSDate *)item;
@end
@interface TimeController : UIViewController <UIPickerViewDelegate>
{
UIDatePicker *timePicker;
}
@property (nonatomic, weak) id <TimeDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIDatePicker *timePicker;
@property (nonatomic, retain) NSDate *activityTime;
@end
.m file
#import "TimeController.h"
@implementation TimeController
@synthesize timePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.timePicker = [NSDate date];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) viewDidDisappear:(BOOL)animated
{
NSLog(@"%@", self.activityTime);
//NSDate *itemToPassBack = [self.timePicker date];
[self.delegate returnTime:self.activityTime];
}
- (IBAction)SelectTime:(id)sender {
self.activityTime = [timePicker date];
}
@end
The error happens when I click select or navigate back. Initially I thought the date picker is not available during viewdiddisappear and exception is thrown. The select button also behave the same and that made me skeptical that there is something else is wrong.
In
viewDidLoad
, this line:assigns an
NSDate
object totimePicker
(which is declared as aUIDatePicker
object).Later, the code tries to get the current date value of the date picker by calling the
date
instance method and (fortunately) fails withunrecognized selector
becauseNSDate
(which is whattimePicker
now points to) has no such instance method.What you actually want to do in
viewDidLoad
is this:Set the
date
property withintimePicker
.