2013-01-10 50 views
0

我正在開發它顯示的開始日期和結束日期的應用程序,但我有帶日期的問題上文本框顯示日期轉換爲字符串的Xcode

一切在這裏工作正常:screenshot

但是當我談談2012年12月30日在輸出,你可以閱讀,上面寫着2012(硬編碼),但在文本字段寫入2013(格式化):

screenshot

如果我嘗試使用「全天」選項,會發生同樣的事情:

screenshot

會分享我的應用程序代碼,以及用於使事情變得更容易

#import "StartEndEventVC.h" 
#import "visitVC.h" 
#import "Functions.h" 



@interface StartEndEventVC() 
{ 
    NSDate     *date; 
    NSDateFormatter   *df; 
    NSString    *selectedCell; 
} 

@property (nonatomic, strong) Functions *funciones; 
@end 


@implementation StartEndEventVC 

@synthesize funciones  = _funciones; 
@synthesize datePicker  = _datePicker, 
      SwitchDate  = _SwitchDate, 
      fecFinDateSE = _fecFinDateSE, 
      fecInicioDateSE = _fecInicioDateSE; 


#pragma mark *** Common methods *** 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Datepicker initial settings 
    self.datePicker.timeZone = [NSTimeZone localTimeZone]; 
    self.datePicker.locale = [NSLocale currentLocale]; 
    self.datePicker.calendar = [NSCalendar currentCalendar]; 

    //Date format initial settings 
    df    = [NSDateFormatter new]; 
    [df setDateFormat:@"EE, dd MMM YYYY HH:mm a"]; 
    [df setTimeZone:[NSTimeZone localTimeZone]]; 

    //initial cell to interact with datepicker 
    selectedCell = @"startDate"; 


    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" 
                      style:UIBarButtonItemStyleBordered 
                      target:self 
                      action:@selector(didBack:)]; 
} 


-(void)estableceFechaCamposTexto{ 

    self.fecInicioDateSE  = [[NSDate alloc]init]; 
    self.fecFinDateSE   = [[NSDate alloc]init]; 

    self.fecInicioDateSE  = [NSDate date]; 
    self.fecFinDateSE   = [self.fecInicioDateSE dateByAddingTimeInterval:60*60]; 


    self.startDateLabel.text = [df stringFromDate:self.fecInicioDateSE]; 
    self.endDateLabel.text  = [df stringFromDate:self.fecFinDateSE]; 
} 


-(void)viewDidAppear:(BOOL)animated{ 
    if ((self.fecInicioDateSE == nil) || (self.fecFinDateSE == nil)) [self estableceFechaCamposTexto]; 

     self.startDateLabel.text = [df stringFromDate:self.fecInicioDateSE]; 
     self.endDateLabel.text  = [df stringFromDate:self.fecFinDateSE]; 
} 




-(Functions *)funciones{ 
    if (!_funciones) _funciones = [[Functions alloc]init]; 
    return _funciones; 
} 


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if ([segue.identifier isEqualToString:@"doneStartEnd"]) { 

     [segue.destinationViewController setFecInicioDateV: self.fecInicioDateSE]; 
     [segue.destinationViewController setFecFinDateV:  self.fecFinDateSE]; 

     NSLog(@"fecha inicio StartEvent: %@", self.fecInicioDateSE); 
     NSLog(@"fecha fin StartEvent: %@", self.fecFinDateSE); 

    } 
} 

#pragma mark - Table view delegate 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    switch (indexPath.row) { 
     case 0: 
      selectedCell = @"startDate"; 
      self.datePicker.date = self.fecInicioDateSE; 

      break; 
     case 1: 
      selectedCell = @"endDate"; 
      self.datePicker.date = self.fecFinDateSE; 
     default: 
      break; 
    } 
} 
#pragma mark - 


#pragma mark *** Custom methods *** 
-(void)comparaFechaInicio{ 

    if ([self.fecInicioDateSE timeIntervalSinceDate:self.fecFinDateSE] >= 0) { 
     if (self.SwitchDate.on)  self.fecFinDateSE = [self.fecInicioDateSE dateByAddingTimeInterval:60*60*24]; 
     else 
      if(!(self.SwitchDate.on)) self.fecFinDateSE = [self.fecInicioDateSE dateByAddingTimeInterval:60*60]; 
    } 

    self.startDateLabel.text = [df stringFromDate:self.fecInicioDateSE]; 
    self.endDateLabel.text  = [df stringFromDate:self.fecFinDateSE]; 

    NSLog(@"Hardcoded date: %@", self.fecInicioDateSE); 
    NSLog(@"formatted date: %@", [df stringFromDate:self.fecFinDateSE]); 
} 

- (void) didBack:(id)sender { 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

#pragma mark - 


#pragma mark *** Button actions *** 
-(IBAction)adjustDate:(id)sender{ 
    if ([selectedCell isEqualToString:@"startDate"]){ 

     self.fecInicioDateSE = [self.datePicker date]; 
     [self comparaFechaInicio]; 
    } 
    else 
     if ([selectedCell isEqualToString:@"endDate"]){ 

      self.fecFinDateSE = [self.datePicker date]; 
      self.endDateLabel.text = [df stringFromDate:self.datePicker.date]; 
     } 
} 

- (IBAction)saveChanges:(id)sender { 
    [self comparaFechaInicio]; 

    [self performSegueWithIdentifier:@"doneStartEnd" sender:self]; 
} 

-(IBAction)changeDateType:(id)sender{ 
    if (self.SwitchDate.on){ 
     self.datePicker.datePickerMode = UIDatePickerModeDate; 
     [df setDateFormat:@"EE, dd MMM YYYY"]; 
    } 
    else{ 
     self.datePicker.datePickerMode = UIDatePickerModeDateAndTime; 
     [df setDateFormat: @"EE, dd MMM YYYY HH:mm a"]; 
    } 

    [self comparaFechaInicio]; 
} 
#pragma mark - 
@end 

回答