调用相同方法的两个UIButton具有不同的结果

时间:2013-07-27 13:04:07

标签: iphone ios objective-c datetime uibutton

我有一个显示时间的UIButton,此时间显示在UIButton的titleLabel属性中。要更改此时间,您可以单击按钮,该按钮会调用此方法来引入日期选择器。

- (IBAction)slidePickerIn:(id)sender {
    // Ok button to dismiss picker
    [self.view bringSubviewToFront:self.datePickerOK];
    // Bring picker to front
    [self.view bringSubviewToFront:self.datePicker];
    // animate entrance of picker and fade in of dismiss button
    [UIView animateWithDuration:.5 animations:^{
        [self.datePicker setFrame:pickerIn];
        self.datePickerOK.alpha = 1;
    }];
}

单击“关闭”按钮时,将调用此方法。它摆脱了日期选择器和关闭按钮,它更新了显示时间的按钮的标题。

- (IBAction)slideDatePickerOut {
    [UIView animateWithDuration:.5 animations:^{
        self.datePickerOK.alpha = 0;
        [self.datePicker setFrame:pickerOut];
    }];

    // Set time correctly
    NSDate *wake = self.datePicker.date;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"hh:mm a"];
    self.wakeUpTime.titleLabel.text = [NSString stringWithFormat:@"%@", [df stringFromDate:wake]];
    self.wakeUpTime.date = wake;
}

屏幕上还有另一个按钮,它也调用slidePickerIn并允许您更改时间。

问题:当我点击这个其他按钮更改显示的日期时,一切正常。但是,当我单击按钮本身时,其中包含显示时间的标题,日期将显示在00:00 AM(默认值),同时显示日期选择器。当日期选择器被解除时,时间正确更新。但正如我所说,使用另一个按钮不会改变显示选择器时的时间值。我真的不确定为什么会这样。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

在你的slidePickerIn中添加:

if(self.datePicker.alpha == 0)
{
    //Add all the methods
}

在slidePickerOut中添加:

if(self.datePicker.alpha == 1)
{
    //Add all the methods
}

现在将在正确的时间内调用UIButtons。

答案 1 :(得分:0)

确保您没有为按钮分配两种方法