当我尝试从日期选择器获取日期时,我正在使用uidatepicker并抛出异常。
.h文件:
#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文件
#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
单击选择或导航后会发生错误。最初我认为在viewdiddisappear期间日期选择器不可用并且抛出异常。选择按钮的行为也相同,这使我怀疑还有其他错误。
答案 0 :(得分:2)
在viewDidLoad
中,此行:
self.timePicker = [NSDate date];
将NSDate
对象分配给timePicker
(声明为UIDatePicker
对象)。
稍后,代码尝试通过调用date
实例方法来获取日期选择器的当前日期值,并且(幸运的是)因unrecognized selector
而失败,因为NSDate
(这是{ {1}}现在指向)没有这样的实例方法。
您在timePicker
中实际想要做的是:
viewDidLoad
在 self.timePicker.date = [NSDate date];
中设置date
属性。