设置 分钟间隔 时 UIDatePicker 的奇怪行为

2024-01-26

以下代码在 iOS 4.3 下显示奇怪的行为(也许其他版本也是如此)。在这个例子中,一个UIDatePicker其日期设置为4 Aug 2011 2:31 PM被展示。这UILabel以下UIDatePicker显示日期以供参考。他们三个UIButtons下面,标记为 1、5、10 设置minuteInterval on the UIDatePicker.

点击 1 - 显示所选日期UIDatePicker to be 4 Aug 2011 2:31 PM,分钟间隔为 1,这是预期的。

点击 5 - 显示所选日期UIDatePicker to be 4 Aug 2011 2:35 PM,分钟间隔为 5,这是预期的(有人可能会认为时间应该向下舍入,但这不是一个大问题)。

点击 10 - 显示所选日期UIDatePicker to be 4 Aug 2011 2:10 PM,分钟间隔为 10。好的,分钟间隔是正确的,但是选择的时间是 2:10?人们预计会是 2:40(如果向上舍入)或 2:30(如果向下舍入)。

BugDatePickerVC.h

#import <UIKit/UIKit.h>

@interface BugDatePickerVC : UIViewController {
    NSDateFormatter *dateFormatter;
    NSDate *date;
    UIDatePicker *datePicker;
    UILabel *dateL;
    UIButton *oneB;
    UIButton *fiveB;
    UIButton *tenB;
}

- (void) buttonEventTouchDown:(id)sender;

@end

BugDatePickerVC.m

导入“BugDatePickerVC.h”

@implementation BugDatePickerVC

- (id) init
{
    if ( !(self = [super init]) )
    {
        return self;
    }

    dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"d MMM yyyy h:mm a";

    date = [[dateFormatter dateFromString:@"4 Aug 2011 2:31 PM"] retain];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Date picker
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 216.0f)];
    datePicker.date = date;
    datePicker.minuteInterval = 1;
    [self.view addSubview:datePicker];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Label with the date.
    dateL = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 230.0f, 300.0f, 32.0f)];
    dateL.text = [dateFormatter stringFromDate:date];
    [self.view addSubview:dateL];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Button that set the date picker's minute interval to 1.
    oneB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    oneB.frame = CGRectMake(10.0f, 270.0f, 100.0f, 32.0f);
    oneB.tag = 1;
    [oneB setTitle:@"1" forState:UIControlStateNormal];
    [oneB   addTarget:self
               action:@selector(buttonEventTouchDown:)
     forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:oneB];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Button that set the date picker's minute interval to 5.
    fiveB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    fiveB.frame = CGRectMake(10.0f, 310.0f, 100.0f, 32.0f);
    fiveB.tag = 5;
    [fiveB setTitle:@"5" forState:UIControlStateNormal];
    [fiveB  addTarget:self
               action:@selector(buttonEventTouchDown:)
     forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:fiveB];

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    // Button that set the date picker's minute interval to 10.
    tenB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    tenB.frame = CGRectMake(10.0f, 350.0f, 100.0f, 32.0f);
    tenB.tag = 10;
    [tenB setTitle:@"10" forState:UIControlStateNormal];
    [tenB   addTarget:self
               action:@selector(buttonEventTouchDown:)
     forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:tenB];

    return self;
}

- (void) dealloc
{
    [dateFormatter release];
    [date release];
    [datePicker release];
    [dateL release];
    [oneB release];
    [fiveB release];
    [tenB release];

    [super dealloc];
}

- (void) buttonEventTouchDown:(id)sender
{
    datePicker.minuteInterval = [sender tag];
}

好的,我可以通过显式设置来改变行为UIDatePicker使用以下代码将日期值四舍五入到分钟间隔:

- (void) handleUIControlEventTouchDown:(id)sender
{
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Set the date picker's minute interval.
    NSInteger minuteInterval  = [sender tag];

    // Setting the date picker's minute interval can change what is selected on
    // the date picker's UI to a wrong date, it does not effect the date
    // picker's date value.
    //
    // For example the date picker's date value is 2:31 and then minute interval
    // is set to 10.  The date value is still 2:31, but 2:10 is selected on the
    // UI, not 2:40 (rounded up) or 2:30 (rounded down).
    //
    // The code that follow's setting the date picker's minute interval
    // addresses fixing the date value (and the selected date on the UI display)
    // .  In the example above both would be 2:30.
    datePicker.minuteInterval = minuteInterval;

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Calculate the proper date value (and the date to be selected on the UI
    // display) by rounding down to the nearest minute interval.
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:date];
    NSInteger minutes = [dateComponents minute];
    NSInteger minutesRounded = ( (NSInteger)(minutes / minuteInterval) ) * minuteInterval;
    NSDate *roundedDate = [[NSDate alloc] initWithTimeInterval:60.0 * (minutesRounded - minutes) sinceDate:date];

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Set the date picker's value (and the selected date on the UI display) to
    // the rounded date.
    if ([roundedDate isEqualToDate:datePicker.date])
    {
        // We need to set the date picker's value to something different than
        // the rounded date, because the second call to set the date picker's
        // date with the same value is ignored. Which could be bad since the
        // call above to set the date picker's minute interval can leave the
        // date picker with the wrong selected date (the whole reason why we are
        // doing this).
        NSDate *diffrentDate = [[NSDate alloc] initWithTimeInterval:60 sinceDate:roundedDate];
        datePicker.date = diffrentDate;
        [diffrentDate release];
    }
    datePicker.date = roundedDate;
    [roundedDate release];
}

注意其中的部分UIDatePicker的日期设置了两次。弄清楚这一点很有趣。

任何人都知道如何关闭动画来调用minuteInterval?点击5然后10时的幻影滚动有点难看。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

设置 分钟间隔 时 UIDatePicker 的奇怪行为 的相关文章

  • userDidAcceptCloudKitShareWith 未被调用

    func application application UIApplication userDidAcceptCloudKitShareWith cloudKitShareMetadata CKShare Metadata 单击共享 cl
  • iOS 10 bug:UICollectionView 收到索引路径不存在的单元格的布局属性

    在 iOS 10 设备上运行我的应用程序时出现以下错误 UICollectionView 收到索引路径不存在的单元格的布局属性 在 iOS 8 和 9 中工作正常 我一直在研究 发现这与使集合视图布局无效有关 我尝试实施该解决方案但没有成功
  • 为什么 NSOrderedSet 不继承 NSSet?

    当然 有序集是集合的更具体的情况 那么为什么NSOrderedSet继承自NSObject而不是NSSet 我通过了界面NSSet你是对的 有序集似乎满足里氏替换原则 http en wikipedia org wiki Liskov su
  • 获取 PHAsset 的本地文件路径

    我希望我的用户能够在 Instagram 上分享一张照片 并且我需要获取该照片的本地文件目录 不过 我将图像作为 PHAsset 获取 而不是 ALAsset 所有其他答案似乎都涵盖了这个主题 查看 PHAsset 文档 我没有看到 本地目
  • Cordova iOS 自定义插件:处理内存警告

    我正在开发一个使用 cordova 2 1 和一些自定义插件的 iOS 应用程序 我试图面对的问题如下 当我展示我的插件时 基本上是一个比内置插件具有更多功能的相机插件 cordova 插件 有时我会收到内存警告并随后卸载 包含 web 视
  • 越狱后,iOS应用程序会以root权限运行吗?

    一旦 iOS 设备越狱 我们就可以构建越狱应用程序 使用 theos 并将其安装在 Applications预加载应用程序以 root 权限运行的目录 如果应用程序是使用 Xcode 构建的 一旦安装 它就会进入 private var m
  • Flutter“运行 pod install 时出错”“Pods-Runner”目标具有传递依赖项

    当尝试运行我的 flutter 项目时 我得到 运行 pod install 时出错 我看到另一个非常相似的post https github com flutter flutter issues 11856但我不确定我是否也遇到同样的问题
  • 如何在 Xcode 8 中更改应用程序显示名称以添加空格

    我试图在 Xcode 8 中的应用程序名称 独立贴纸包 中添加一个空格 我在这里看到的解决方案是更改产品名称 在包装中 或更改 捆绑显示名称 我更改了产品名称 但没有起作用 我清理 重建 重置模拟器中的内容和设置 并注销 xcode 重新登
  • 如何解决这个错误? - 类“ViewController”没有初始化程序

    我创造了UILabel快速编程 但它给了我以下错误 类 ViewController 没有初始值设定项 Code class ViewController UIViewController let lbl LastName UILabel
  • Objective C 中最好的多线程方法?

    我正在开发一个 iPad 应用程序 目前正在努力寻找多线程的最佳方法 让我用一个简化的例子来说明这一点 我有一个包含 2 个子视图的视图 一个目录选择器和一个包含所选目录中所有图像缩略图的图库 由于 下载 和生成这些缩略图可能需要相当长的时
  • GMSMarker 不透明度动画不重复

    我正在尝试使带有自定义图标的 GMSMarker 以衰减的动画不透明度闪烁 动画应该重复几次 但事实并非如此 它只执行一次转换 然后就停止了 这种情况仅在对不透明度属性进行动画处理时发生 在对其他属性进行动画处理时效果很好 这是代码 GMS
  • AVAudioPlayer 在 iPhone 5 中无法工作

    我正在开发一个 iPhone 应用程序 在这个应用程序中 我使用 AVAudioPlayer 来播放音频 这是我播放声音的代码 NSString resourcePath NSBundle mainBundle resourcePath r
  • SpriteKit内存管理预加载缓存和fps问题

    我的问题非常简单 根据苹果文档 您可以在呈现场景之前将纹理预加载到 RAM 中 如下所示 SKTextureAtlas atlas SKTextureAtlas atlasNamed effect circle explode SKText
  • iOS 4.2 - 打电话后返回应用程序

    我可以使用以下命令在我的应用程序中成功发起电话呼叫 UIApplication sharedApplication openURL NSURL URLWithString tel 123456789 但是 通话结束后是否可以自动返回应用程序
  • Apple IOS 上的 C# 应用程序 [已关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我有基于 C Net 的应用程序 有什么方法可以在 Apple IOS 上运行这些应用程序吗 我没有资
  • 自定义 UITableViewCell 中按钮上的 IBAction

    使用 iOS 5 我有一个场景 我必须使用自定义单元格创建一个 tableView 自定义单元格有一个名为 TainingCellController 的控制器 UITableViewCell 的子类和一个 NIB 文件 TrainingC
  • 使用 Objective-C 将 XMP 数据嵌入到 PNG

    我需要将自定义 XMP 文件嵌入到 iOS 应用程序中的 PNG 中 到目前为止 我能做的就是编译 Adob e XMP 工具包 生成 Xcode 项目 然后正确编译该项目 从那里我不知道如何在我的 Xcode 项目中使用该库以及如何使用它
  • 捆绑 pathsForResourcesOfType:inDirectory:

    在我的应用程序中 我有很多图片 分为几个类别 以下是项目内和我的硬盘上的应用程序树 ApplicationName Resources Thumbs Images Buttons Default png 在拇指文件夹中 我有很多 png 文
  • Xcode 8 - 删除了一些按钮边框

    我刚刚将 Xcode 版本从 7 3 更新到 8 0 一些按钮边框消失了 代码看起来很好 所以我真的不知道各层发生了什么 顺便说一句 在其他一些控制器中我可以看到图层边框 self button layer borderColor bord
  • 如何添加私有 Spec Repo 以使用私有 Pod?

    我完成了这个教程http guides cocoapods org making private cocoapods html http guides cocoapods org making private cocoapods html但

随机推荐

  • Spritekit - 不从 SKTextureAtlas 加载 @3x 图像

    由于我的示例项目被删除 我认为这会更容易测试 我将发布一些代码和图像来说明我的观点 这是示例图像 我的图集设置 我的启动图像设置 我将这些精灵添加到场景中的代码 override func didMoveToView view SKView
  • 如何在 Blazor Hybrid 中的 muddatagrid 列中增加模型的值

    如何增加 muddatagrid 列中模型的值 如果我按 olus 图标 它将增加所有数量 建议我一种可以与 onclick eventcallback 一起使用的方法 我还需要将其增加 0 5
  • 如何模拟 URLSession.DataTaskPublisher

    我该如何嘲笑URLSession DataTaskPublisher 我有课Proxy需要注入一个URLSessionProtocol protocol URLSessionProtocol func loadData from url U
  • 如何将密码从文件传递到mysql命令?

    我有一个 shell 脚本 它使用外部文件中的一个参数调用 mysql 命令 它看起来像这样 我也在其他资源中看到了这个示例 mysql user root password cat root mysql 有点不工作 无法连接到 MySQL
  • Android 操作栏(如 Twitter 示例)

    实现 Twitter 示例 UI 模式等操作栏的最佳方法是什么 Android 版 Twitter 深入了解 Android 不断演变的 UI 模式 模式4 操作栏http android developers blogspot com 2
  • 在 Powershell 中写入十六进制转义字符

    有没有办法在Powershell中写这样的东西 Linux 将与 Perl 一起使用 char foo x41 我需要在我的一个程序中输入一些不可打印的字符 你可以这样做将 int 转换为 char 带十进制数 foo 65 as char
  • HttpClient GetAsync 方法 403 错误

    我正在尝试简单地显示 github 存储库 网址 https api github com search repositories q pluralsight https api github com search repositories
  • 使用cmd命令打开pwsh而不退出

    我正在尝试启动一个 Powershell 窗口 使用以下命令启动 ssh 会话 pwsh exe noexit Command ssh
  • 如何在 LINQ 中对单个联接中的多个字段进行左联接

    我正在尝试对 LINQ 执行这个简单的 sql 查询 但它给了我错误 这是需要转换为 LINQ 的 SQL 查询 DECLARE groupID int SET groupID 2 SELECT FROM dbo Person p LEFT
  • C++ 中两个向量的逐元素乘法

    我试图用两个向量进行以下数学运算 v1 a1 a2 a3 a4 a5 v2 b1 b2 b3 b4 b5 想要计算 v a2 b2 a3 b3 a4 b4 a5 b5 请注意 我不想要新向量中的第一个元素 我想知道是否有一种比 for 循环
  • Mongo shell 中的 NumberLong 算术

    如何在 Mongo shell 中对 NumberLong 值执行精确算术 据我了解 Javascript 只有一种数字类型 number 通常限制为 54 位浮点精度 使用 例如 标准加法的直接算术显示将强制转换为较低精度类型 gt Nu
  • 向 geom_bar() / geom_col() 条添加图案或纹理?

    有时 我需要某种用于 geom bar geom col 条的图案或纹理 即用于黑白打印 例如 以下内容对于某些人来说可能很难查看 library ggplot2 library dplyr warn conflicts FALSE lib
  • 设置标头并使用 $http POST 发送数据到 pocket api 返回 CORS

    无法向 pocket api 发送 http post 请求以获取请求令牌 我已经拿到消费者密钥了 问题似乎出在设置标头和发送请求中的数据 在浏览器中查看请求时 不会显示任何标头和数据 配置请求 var req method POST ur
  • 从整数的商中获取双精度值

    int velMperMin 667 int distM 70 double movT distM velMperMin 60 movtT必须等于6 30 但它是0 您需要将除法的操作数之一转换为双精度值 像这样 double movT d
  • 使用 UMAP 和 HDBScan 进行集群

    我有大量的文本数据 大约有 5000 人输入 我使用 Doc2vec 为每个人分配了一个向量 使用 UMAP 缩减为二维 并使用 HDBSCAN 突出显示其中包含的组 目的是突出具有相似主题相似性的组 这导致了如下所示的散点图 这看起来可以
  • Gitlab CI如何部署最新到特定目录

    我在 Gitlab 中有两个项目 其中一个是另一个项目 我们称这个存储库为 main 的子模块 我们称其为 前端模板 我已经为 frontend templates 存储库设置了 Gitlab CI 构建 问题是我不需要测试或构建 我只需要
  • 将 UIView 中的标签居中

    将标签居中的最佳方法是什么UIView 如果你做了类似的事情 UILabel myLabel UILabel alloc initWithFrame CGRectMake view frame origin x 2 view frame o
  • Flask 只能看到通过curl 发送的多个参数中的第一个参数

    我正在使用curl 向需要多个查询参数的Flask 路由发出请求 但是 日志仅显示 url 中的第一个参数 Flask 看不到第二个参数 出了什么问题 app route path methods GET def foo print req
  • 从 .NET 3.5 WCF Web 服务 (REST) 返回 JSON 和 XML 格式

    我有一个返回 XML 响应的现有 Web 服务 我想添加一些返回 JSON 的新方法 我是否必须创建一个以 JSON 形式返回的单独 Web 服务 还是可以混合使用 如果我使用 ResponseFormat WebMessageFormat
  • 设置 分钟间隔 时 UIDatePicker 的奇怪行为

    以下代码在 iOS 4 3 下显示奇怪的行为 也许其他版本也是如此 在这个例子中 一个UIDatePicker其日期设置为4 Aug 2011 2 31 PM被展示 这UILabel以下UIDatePicker显示日期以供参考 他们三个UI