繁体   English   中英

从 Xcode (Swift) 中的按钮 IB Function 将时间戳打印到 label

[英]Printing a timestamp to a label from a button IB Function in Xcode (Swift)

我想创建一个时间戳,当在一个视图 controller 中按下按钮时触发当前时间,并让它在另一个视图 controller 中打印到 label。

我正在使用 Xcode 11 和最新版本的 swift。

我猜这是我到目前为止的 function 但不知道如何将它实现到按钮或 label 我希望它打印到:

func getDateDayAndTime(timestamp: NSNumber) -> String {
        let date  = Date(timeIntervalSince1970: Double(truncating: timestamp)/1000)
        let calendar = Calendar.current

        if calendar.isDateInToday(date) {
            let dateFormatter = DateFormatter()
            dateFormatter.timeZone = NSTimeZone.local
            dateFormatter.dateFormat = "hh:mm a"
            let time = dateFormatter.string(from: date)
            return time
        }else if calendar.isDateInYesterday(date) {
            return "Yesterday"
        }
        else if calendar.isDateInWeekend(date) {
            let component = calendar.component(Calendar.Component.weekday, from: date)
            return self.getDay(value: component)
        } else {
            let dateFormatter = DateFormatter()
            dateFormatter.timeZone = NSTimeZone.local
            dateFormatter.dateFormat = "dd/MM/YY"
            let time = dateFormatter.string(from: date)
            return time
        }
    }

我们可以将timestamp值从 ViewController 发送到 SecondViewController,如下所示,

import UIKit

class ViewController: UIViewController {

    @IBAction func getTimeStampButtonAction(_ sender: Any) {
        let timestampVal = NSDate().timeIntervalSince1970
        let myTimeInterval = TimeInterval(timestampVal)
        let timeStamp = NSDate(timeIntervalSince1970: TimeInterval(myTimeInterval))
        let timeStampString = stringFromDate(timeStamp as Date)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let homeView  = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
        homeView.timeStamp = timeStampString
       self.navigationController?.pushViewController(homeView, animated: true)

    }

    func stringFromDate(_ date: Date) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "dd MMM yyyy HH:mm" //yyyy
        return formatter.string(from: date)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

SecondViewController.swift

import UIKit

class SecondViewController: UIViewController {
    @IBOutlet weak var TimeStampLabel: UILabel!
     var timeStamp: String!

    override func viewDidLoad() {
        super.viewDidLoad()
        TimeStampLabel.text = timeStamp
    }
}

Function 返回当前时间:

  func getTodayString() -> String{

            let date = Date()
            let calender = Calendar.current
            let components = calender.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)

            let year = components.year
            let month = components.month
            let day = components.day
            let hour = components.hour
            let minute = components.minute
            let second = components.second

            let today_string = String(year!) + "-" + String(month!) + "-" + String(day!) + " " + String(hour!)  + ":" + String(minute!) + ":" +  String(second!)

            return today_string

        }

按下按钮:

@IBAction func buttonPressed(_ sender: Any) {
yourLabel.text = getTodayString()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM