首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分段控制:控制重复间隔

分段控制:控制重复间隔
EN

Stack Overflow用户
提问于 2016-06-20 17:07:12
回答 1查看 153关注 0票数 0

我不知道如何让用户选择本地通知的重复间隔。我已经链接到两个详细描述问题的图像。

This is the image of the file where I need to insert the choose from the segmented control

And this is the image of the file where the segmented control is placed.

EN

回答 1

Stack Overflow用户

发布于 2016-06-20 19:41:50

我查看了您的代码,一种解决方案是将segmentedControl的值作为参数添加到TodoItem中,就像您现在已经在gentag参数中所做的那样。

然后,在您的addItem方法中,您只需要将您现在拥有的Int值转换为一个可以传递给notification.repeatIntervalNSCalendarUnit

为了保持这一点,您可以创建一个知道如何从Int值转换为NSCalendarUnit值的新Enum,然后您的gentag参数就可以是这种类型。

因此,在你拥有TodoSchedulingViewController的文件中,你可以在文件的顶部写下类似这样的代码:

代码语言:javascript
复制
import UIKit

enum RepeatInterval: Int {
    case None = 0
    case Daily = 1
    case Weekly = 2

    func toCalendarUnit() -> NSCalendarUnit {
        switch self {
        case .None:
            return NSCalendarUnit(rawValue: 0)
        case .Daily:
            return NSCalendarUnit.Day
        case .Weekly:
            return NSCalendarUnit.Weekday
        }
    }
}

class TodoSchedulingViewController: UIViewController {
...

然后您可以像这样使用它:

在你的ViewController

代码语言:javascript
复制
let todoItem = TodoItem(deadline:....,
                        title:....,
                        gentag: RepeatInterval(rawValue: segmentedControl.selectedSegmentIndex)!,  //You can force unwrap here, because you know the value is always one you get from your segmentedControl
                        UUID......)

在您的TodoList类中:

代码语言:javascript
复制
...
notification.userInfo = ["title" : item.title, "UUID" : item.UUID]
notification.repeatInterval = item.gentag.toCalendarUnit()

希望这是有意义的。

Oh...and下次,请发布您的实际代码而不是图像,这样可以更容易地快速了解正在发生的事情,并更快地将代码片段复制到答案中:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37918528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档