UI日期选择器未在标签中显示

时间:2014-09-05 04:47:16

标签: ios objective-c uidatepicker

当我在我的应用程序中转到viewcontroller时,我有两个日期选择器。两者都有标签,如果其中任何一个被移动,它们会相应地更新。

开始日期设置为自动成为今天的日期。虽然它可以很好地保存到Core Data,但它不会在标签中显示,除非我将其更改为不同的日期。

基本上,当我加载该屏幕时,标签应该具有今天的日期,如果我在日期选择器中更改它,则会更改。

我认为这是一个简单的俯视,但我无法弄清楚它是什么,任何帮助都会受到赞赏。

- (void)viewDidLoad
{
    [super viewDidLoad];

    svos = self.scrollView.contentOffset;

    [self.startDate addTarget:self action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];
    [self.endDate addTarget:self action:@selector(datePickerChanged1:) forControlEvents:UIControlEventValueChanged];

    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    if(!appDelegate.dateProductionDroneStartDate)
        [self.startDate setDate:[NSDate date]];

    else
    {
        [self.startDate setDate:appDelegate.dateProductionDroneStartDate];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM-dd-yyyy "];
        NSString *strDate = [dateFormatter stringFromDate:self.startDate.date];
        self.productionDroneStartDate.text = strDate;
    }

    NSDate *now = [NSDate date];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:1];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0];

    if(!appDelegate.dateProductionDroneEndDate)
        [self.endDate setDate:newDate2];
    else
    {
        [self.endDate setDate:appDelegate.dateProductionDroneEndDate];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM-dd-yyyy "];
        NSString *strDate = [dateFormatter stringFromDate:self.endDate.date];
        self.productionDroneEndDate.text = strDate;
    }

    self.txtProductionName.text = appDelegate.strProductionName;
    self.txtProductionScene.text = appDelegate.strProductionScene;
    self.txtProductionReel.text = appDelegate.strProductionReel;

    self.txtProductionName.delegate = self;
    self.txtProductionReel.delegate = self;
    self.txtProductionScene.delegate = self;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];

}

2 个答案:

答案 0 :(得分:0)

到目前为止我从你的问题和代码中得到了什么,简单的错误如下: -

- (void)viewDidLoad
{
 [super viewDidLoad];
 ....
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy "];
 NSString *strDate = [dateFormatter stringFromDate:self.startDate.date];
 AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
 if(!appDelegate.dateProductionDroneStartDate)   //As initially this will be true and current date will be set.
    [self.startDate setDate:[NSDate date]];
     //So as you set the current date in self.startDate but not in label.
     //Added below line for setting your label
 else
 {
    [self.startDate setDate:appDelegate.dateProductionDroneStartDate];
 }
 self.productionDroneStartDate.text = strDate;
 ......
}

这可以让你进一步努力。

答案 1 :(得分:0)

为什么你不能在开始时用今天的日期设置标签,甚至不用触摸Picker。然后,当您建议移动选择器时,标签将按预期更新。