0

目标是让用户设置一个截止日期并将这个截止日期保存到一个对象中。

这是对象的 saveListItem 方法:

+ (void)saveListItem:(PFUser *)theUser withName:(NSString *)theListItem withDate:(NSDate *)theDueDate
{
    PFObject *listObject = [[PFObject alloc] initWithClassName:@"ListItem"];
    [listObject setObject:[PFUser currentUser] forKey:@"author"];
    [listObject setObject:theListItem forKey:@"listName"];
    [listObject setObject:theDueDate forKey:@"dueDate"];
    [listObject save];
    NSLog(@"%@", listObject);

}

这里是它的名称:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *saveDate = [[UIBarButtonItem alloc]
                                 initWithTitle:@"Save Date"
                                 style:UIBarButtonItemStyleDone
                                 target:self
                                 action:@selector(saveList:)];
    self.navigationItem.rightBarButtonItem = saveDate;
    pick = [[UIDatePicker alloc] init];
    [pick setFrame:CGRectMake(0,200,320,120)];
    [pick addTarget:self action:@selector(updateDateLabel:) forControlEvents:UIControlEventValueChanged];
}

-(IBAction)saveList:(id)sender {

    [ListItemObject saveListItem:[PFUser currentUser] withName:listFieldText withDate:pick.date];
}

-(IBAction)updateDateLabel:(id)sender {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterLongStyle];
    [formatter setTimeStyle:NSDateFormatterMediumStyle];
    dateLabel.text = [formatter stringFromDate:pick.date];
}

我检查了日期选择器的参考出口,它被设置为选择。

正在保存的对象是当前日期,而不是选择器上选择的日期。

4

1 回答 1

0

您的代码没有做错任何事情。

但是,这是最坏的修复情况,

-(void)updateDateLabel:(id)sender {

   formatter = [[NSDateFormatter alloc] init];
   [formatter setDateStyle:NSDateFormatterLongStyle];
   [formatter setTimeStyle:NSDateFormatterMediumStyle];
   dateLabel.text = [formatter stringFromDate:((UIDatePicker *)sender).date];
}       

-(IBAction)saveList:(id)sender {

    NSDate *selectedDate = [formatter dateFromString:dateLabel.text];
    [ListItemObject saveListItem:[PFUser currentUser] withName:listFieldText withDate:selectedDate];
}
于 2013-03-17T22:21:20.527 回答