2012-10-22 47 views
0

我使用uidatepicker並拋出異常,當我嘗試從日期選擇器獲取日期。uidatepicker [__NSDate date]:無法識別的選擇器發送到實例

.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 selectorNSDate(這是什麼timePicker現在指向)沒有這樣的實例方法。


你真的想在viewDidLoad做的是這樣的:

self.timePicker.date = [NSDate date]; 

集中timePickerdate財產

相關問題