2013-11-28 92 views
1

我有UIViewControllerUIDatePicker裏面。我在頭類IBOutlet屬性連接UIDatePicker,因爲我用我的應用程序的故事板:UIDatePicker不更改值

#import <UIKit/UIKit.h> 
@protocol CreationDateDelegate <NSObject> 
@optional 
    -(void)selectedDateIs:(NSString*)dateValue; 
@end 

@interface SelectCreationDateViewController : UIViewController 
{ 
    IBOutlet UIDatePicker *datePicker; 
} 
@property (nonatomic,assign)id<CreationDateDelegate>creationDateDelegate; 
@property (nonatomic,retain) IBOutlet UIDatePicker *datePicker; 

@end 

--- .M -----

#import "SelectCreationDateViewController.h" 
@interface SelectCreationDateViewController() 
{ 
    NSString *dateFromString; 
} 
@end 

@implementation SelectCreationDateViewController 
@synthesize creationDateDelegate,datePicker; 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    { 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    self.title = @"Select creation date"; 
    [super viewDidLoad]; 

    [datePicker setHidden:NO]; 
    datePicker.timeZone = [NSTimeZone localTimeZone]; 
    [datePicker setDatePickerMode:UIDatePickerModeDate]; 

    [datePicker addTarget:self action:@selector(changeValueOfDatePicker:) forControlEvents:UIControlEventValueChanged]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void)viewWillDisappear:(BOOL)animated{ 
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
    NSString *dateFromString = [formatter stringFromDate:datePicker.date]; 
    [self.creationDateDelegate selectedDateIs:dateFromString]; 
} 

-(void)changeValueOfDatePicker:(id)sender{ 
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
    dateFromString = [formatter stringFromDate:datePicker.date]; 
    [self.creationDateDelegate selectedDateIs:dateFromString]; 
    NSLog(@"Date from string value equal %@",dateFromString); 
} 
@end 

爲什麼每一次當我改變DatePicker返回值的值不會更改?有任何想法嗎?另外,讓我知道如果我在代碼中犯了任何錯誤。

+0

您的代碼changeValueOfDatePicker:方法在哪裏? –

+1

其中聲明瞭UIDatePicker的代表 – codercat

+0

@iDev沒有UIDatePicker的代理。 –

回答

0

設置一些dateformate也

-(void)changeValueOfDatePicker:(id)sender{ 

     NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
     [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; 

     dateFromString = [formatter stringFromDate:datePicker.date]; 
     [self.creationDateDelegate selectedDateIs:dateFromString]; 
     NSLog(@"Date from string value equal %@",dateFromString); 
    } 
+0

奇怪,但它的工作原理。你能解釋爲什麼嗎? – sonoDamiano

+0

[formatter stringFromDate:datePicker.date];由defualt沒有datFormate設置爲NSDateFormatter。您手動需要設置一些dateFromat,然後只有dateFormatter能夠執行關聯的操作。更好的閱讀這個鏈接https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSDateFormatter_Class/Reference/Reference.html –

+0

謝謝你的幫助信息,問候 – sonoDamiano

0

修復它加入這行代碼

僅當您使用iOS7 - 在法(無效)viewDidLoad方法

[self.datePicker setDate:[NSDate date] animated:YES]; 
+0

我正在使用iOS7,但它不起作用 – sonoDamiano