Reputation: 19
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.
Upvotes: 0
Views: 1314
Reputation:
In viewDidLoad
, this line:
self.timePicker = [NSDate date];
assigns an NSDate
object to timePicker
(which is declared as a UIDatePicker
object).
Later, the code tries to get the current date value of the date picker by calling the date
instance method and (fortunately) fails with unrecognized selector
because NSDate
(which is what timePicker
now points to) has no such instance method.
What you actually want to do in viewDidLoad
is this:
self.timePicker.date = [NSDate date];
Set the date
property within timePicker
.
Upvotes: 2