【问题标题】:How to use Date or Calendar to change the value in UITextView?如何使用日期或日历更改 UITextView 中的值?
【发布时间】:2017-09-11 18:59:22
【问题描述】:

我有一个 UITextView,我需要每天为数组中的下一个“字符串”更改 2 次(每 12 小时一次)。 我这里有一些需要与 Date 和 UITextView 一起使用的数组。 我需要如何使用日历? 我的简单问题是“我需要做什么?”

@IBOutlet var ThisTextView: UITextView!
var array = ["a","b","c","d"]
var lastDate: Date?
var currentDate = Date()

    override func viewDidLoad() {
        super.viewDidLoad()

        if currentDate == lastDate {
            // we do nothing 
        } else {
            // we change string to the next in array using some function 
                    // or we write this function here
        }

【问题讨论】:

标签: swift date calendar


【解决方案1】:

这是我想出的代码:

// This is YOUR "lastDate", please supply your own value
let mySuppliedDate = Date()

// You should have your own date to compare to and convert it to seconds
let compareDate:Int = Int(mySuppliedDate.timeIntervalSince1970)

// Current date (today's date)
let currentDate: Date = Date()


let dateFormatter: DateFormatter = DateFormatter()
// Customize the dateStyle property to suit your needs (date format)
dateFormatter.dateStyle = .medium

// This will give you the current time in seconds, or what is known as the (UNIX) Epoch time
// We cast it to an Int because it returns a Double
let currentDateInSecs: Int = Int(currentDate.timeIntervalSince1970)

// Formula: seconds * minutes per hour * no. of hrs
let triggerTime = 60 * 60 * 12

if (currentDateInSecs - compareDate) >= triggerTime {
    // 12 hrs or more have passed

    // Update the Label
    ThisTextView.text = dateFormatter.string(from: currentDate)

}

// else less than 12 hrs have passed

// We do nothing

请注意,您必须向 mySuppliedDate 提供您自己的值。

我希望这会有所帮助!

【讨论】:

  • 您的代码没问题,但我如何更新标签?也许是这样的: var i = 0 func action() { thisTextView.text = array[i] i = (i + 1) % array.count }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 2014-04-13
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多