【问题标题】:Two UIButtons calling the same method have different results调用相同方法的两个 UIButton 有不同的结果
【发布时间】:2013-07-27 13:04:06
【问题描述】:

我有一个显示时间的 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(默认值),同时显示日期选择器。当日期选择器被关闭时,时间会正确更新。但正如我所说,使用另一个按钮不会更改显示选择器时的时间值。我真的不确定为什么会这样。任何帮助将不胜感激。

【问题讨论】:

  • 次要细节——创建和格式化日期格式化程序几乎需要半秒钟。你应该把它变成静态的,并且只做一次。
  • 这是设置断点的最佳时机。在 -[UILabel setText:] 中添加一个符号断点,并在您点击出现问题的按钮之前启用它。某些东西正在发送带有错误值的 setText: 消息。当代码在调试器中中断时,您将能够跟踪导致更改的代码。
  • @HalR 你的意思是我应该将格式化程序保存在一个属性中,这样我就不必每次都创建它?
  • @BoredAstronaut 谢谢,这很有帮助。确实,当我单击按钮时,会调用 setText,但在调试器上,我只看到来自 objc 类的疯狂代码,当我走出它们时,我从未接触到任何自己的代码。所以基本上不知道什么叫这个 setText
  • 是的,保留格式化程序。作为财产这样做会很好。

标签: iphone ios objective-c datetime uibutton


【解决方案1】:

在你的 slidePickerIn 中添加:

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

在你的 slidePickerOut 添加:

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

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

【讨论】:

    【解决方案2】:

    确保您没有将这两种方法都分配给按钮

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多