uidatepicker [__NSDate date]:发送到实例的无法识别的选择器

时间:2012-10-22 00:31:43

标签: ios xcode4 nsdate uidatepicker nsdatepicker

当我尝试从日期选择器获取日期时,我正在使用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期间日期选择器不可用并且抛出异常。选择按钮的行为也相同,这使我怀疑还有其他错误。

1 个答案:

答案 0 :(得分:2)

viewDidLoad中,此行:

self.timePicker  = [NSDate date];

NSDate对象分配给timePicker(声明为UIDatePicker对象)。

稍后,代码尝试通过调用date实例方法来获取日期选择器的当前日期值,并且(幸运的是)因unrecognized selector而失败,因为NSDate(这是{ {1}}现在指向)没有这样的实例方法。


您在timePicker中实际想要做的是:

viewDidLoad

self.timePicker.date = [NSDate date]; 中设置date属性