设置 分钟间隔 时 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 的奇怪行为 的相关文章

  • 使用线程安全单例初始化代码时代码执行停止

    为了利用全局变量和方法 我实现了 Singleton 作为一种健康的编码实践 我跟着苹果文档 http www johnwordsworth com 2010 04 iphone code snippet the singleton pat
  • ios 7 - 强制视图布局翻转到 RTL,无需更改语言

    我的应用程序中有一些视图需要能够显示 LTR 和 RTL 内容 但不能同时显示 并且它必须与一般应用程序布局方向无关 意思是 我希望能够告诉特定视图将其布局从 LTR 翻转到 RTL 反之亦然 无需更改系统语言 我对此进行了相当多的研究 但
  • React Native 中文本的图像识别

    这可能是一个疯狂的问题 但我已经看到应用程序完成了 是否有任何类型的 API 可用于识别图像中的文本 Chase 识别支票上的数字的方式 或者是否有一个 API 可用于搜索 比如谷歌 基于图像的信息 例如 如果我拍了一张企业徽标的照片 谷歌
  • Objective C renderInContext 在后台线程上崩溃

    我有一个应用程序 其中屏幕连续在后台线程中捕获 这是代码 UIImage captureScreen UIWindow keyWindow UIApplication sharedApplication keyWindow CGRect r
  • SwiftUI:获取动态背景颜色(深色模式或浅色模式)

    有没有一种方法可以系统地访问 SwiftUI 视图的标准动态背景颜色 无论用户处于浅色模式还是深色模式 例如 我知道以下内容可用于获取主要 例如文本 颜色 let textColor Color primary 但我没有看到任何类似的背景颜
  • 从 CocoaPods 添加 pod 时,架构 x86_64 的重复符号

    我正在尝试使用谷歌分析 https developers google com analytics devguides collection ios v3 进入我的应用程序 但通过 CocoaPod 添加后立即收到此错误 以前我的 Pod
  • 动态增加UITableViewCell中UILabel的高度?

    我有一个 UITableView 其中显示一个自定义单元格 我的单元格有两个标签和一个视图 如下图所示 我已经像这样给出了左视图的约束 项目标签限制 中心视图约束 右视图的约束 I am using a bean class to stor
  • 循环缓冲区录音 iOS:可能吗?

    我的一个客户想要连续录制音频 当他单击 提交 时 他只想提交最后 10 秒的内容 所以他想要连续记录并且只保留最后 x 秒 我认为这需要类似循环缓冲区的东西 但是 作为 iOS 的新手 它看起来像AVAudioRecorder只能写入文件
  • 进入/退出编辑模式时重绘 UITableViewCell

    我有一个表格视图 其中根据表格是否正在编辑 单元格的构建方式有所不同 具体来说 处于编辑模式时选择样式为无 非编辑模式时选择样式为蓝色 当我从一个单元转换到另一个单元时 我注意到某些单元格没有更新 快速的日志记录告诉我 即使单元格的外观发生
  • 显示不带字母的数字键盘

    iOS 默认数字键盘中是否有隐藏数字下方字母的选项 对于某些电话语言 键盘显示时不带字母 抱歉 你所要求的是不可能的 这取决于键盘语言 只有用户可以更改键盘语言 我希望这能帮到您
  • iOS:调用 Objective-C 方法的处理开销是多少?

    我正在编写一些实时音频处理代码 该代码将在音频单元的渲染回调中执行 该线程处于系统识别的最高优先级 Apple 指示最大限度地减少此调用中进行的处理量 他们的建议之一是避免 Objective C 方法调用 But why 调用 Objec
  • BUG - 在 IOS 中没有选择标签的完成按钮

    我正在使用最新的离子并有一个简单的选择标签
  • 部署目标是什么意思?

    这是我假设的一个非常简单的问题 有人可以告诉我部署目标是什么意思吗 如果我选择 iOS 10 是否意味着只有 iOS 10 的用户才能下载该应用程序 选择较低的部署目标是否不好 另外 继续部署目标 是否不建议在较低的部署目标上运行 假设您已
  • TDD iOS 教程 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 您好 我正在寻找非常好的 iOS TDD 教程 请您帮助我 什么是最好的 iOS TDD 书籍 博客
  • CLLocation Manager如何在一定距离后更新

    我正在使用 CLLocationManager didupdatelocations 如下所示 func locationManager manager CLLocationManager didUpdateLocations locati
  • iPhone iOS 保存从 UIImageJPEGRepresentation() 获得的数据第二次失败:ImageIO: CGImageRead_mapData 'open' failed

    我的 UIImage 操作遇到了一个奇怪的问题 我正在进行保管箱同步 并且必须将我的图像存储为本地文件 为此 我使用以下命令保存它们UIImagePNGRepresentation image or UIImageJPEGRepresent
  • 允许的 APNS 持续连接数量是多少?

    我正在尝试编写服务器端代码来为我的应用程序发送推送通知 根据 Apple 的建议 我计划保留连接并根据需要发送推送通知 Apple 还允许打开和保留多个并行连接以发送推送通知 您可以与同一网关或多个网关实例建立多个并行连接 为此 我想维护一
  • 从数组中获取随机字符串[重复]

    这个问题在这里已经有答案了 我试图从数组 firstArray 中获取随机字符串并将其打印在 UILabel label 中 我似乎无法弄清楚并且出现错误 感谢您的帮助 我尝试搜索但找不到任何最新的教程 方法 import UIKit cl
  • 当我从我转向的视图控制器返回时,为什么我的 UITableView 的格式完全出错了?

    我有一个UITableView使用自定义单元格 其中有一些标签可以动态决定单元格的高度 当我点击一个单元格并转到一个新的视图控制器时 返回后所有单元格的格式完全混乱 我无法弄清楚是什么导致了它 这是细胞通常的样子 我对它们设置了一些非常基本
  • 自定义字体显示在 IB 中,但不显示在模拟器中

    我已经设置了一个UITextView and a UILabel使用自定义字体 它是垂直镜像的蒙古文字体 但我还添加了英文文本 以便您可以看到效果 这些文字显示在 Interface Builder 中 但在模拟器中大部分字符都在UITex

随机推荐

  • 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