0

我的Model文件:

起初我的TimeModel

import Foundation
class TimeModel {
    var hrs = ""
    var min = ""
    var sec = ""
    var time = ""
    
    init(hrs: String, min: String, sec: String, time: String) {
        self.hrs = hrs
        self.min = min
        self.sec = sec
        self.time = time
    }
}

然后我有一个data文件:

import Foundation

class Data {
    
    class func getDataTime() -> [TimeModel] {
        var dataTime = [TimeModel]()
               
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,0", time: "Custom Time"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,00013", time: "1/8000"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,00016", time: "1/6400"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,0002", time: "1/5000"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,00025", time: "1/4000"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,00031", time: "1/3200"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,0004", time: "1/2500"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,0005", time: "1/2000"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,00062", time: "1/1600"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,0008", time: "1/1250"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,001", time: "1/1000"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,00125", time: "1/800"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,00156", time: "1/640"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,002", time: "1/500"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,0025", time: "1/400"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,00313", time: "1/320"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,004", time: "1/250"))
        dataTime.append(TimeModel(hrs: "0", min: "0", sec: "0,005", time: "1/200"))
        return dataTime
    }
        
}

这是我的 PickerView 代码:

    import UIKit

class TimeModelPicker: UIView, UIPickerViewDataSource, UIPickerViewDelegate {
    
    var timeModelData = [TimeModel]()
   

        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }
        
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return timeModelData.count
        }

        func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
            let pickerLabel = UILabel()
            pickerLabel.textColor = UIColor.white
            pickerLabel.text = timeModelData[row].time
            pickerLabel.font = UIFont.boldSystemFont(ofSize: 25)
            pickerLabel.textAlignment = NSTextAlignment.center
            return pickerLabel
        }
    
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
                 
        let defaults = UserDefaults.standard
            defaults.set(row, forKey: "saveTimePicker")
        }
}

最后是我的 VewController 中的代码:

    import UIKit

class PickerViewTimeController: UIViewController {

    @IBOutlet weak var pickerViewTime: UIPickerView!
    @IBOutlet weak var pickerViewLabel: UILabel!
    @IBOutlet weak var okButton: UIButton!
    @IBOutlet weak var timeStd: UITextField!
    @IBOutlet weak var timeMin: UITextField!
    @IBOutlet weak var timeSec: UITextField!
        
    var dataPickerViewTime: TimeModelPicker!
    
    
        
//unwind / ok Button
    @IBAction func unwindtoCalculate(_ sender: UIStoryboardSegue){
        dismiss(animated: true, completion: nil)
    }
 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        dataPickerViewTime = TimeModelPicker()
        dataPickerViewTime.timeModelData = Data.getDataTime()
        pickerViewTime.delegate = dataPickerViewTime
        pickerViewTime.dataSource = dataPickerViewTime
        
        if let selectedRow = UserDefaults.standard.object(forKey: "saveTimePicker") as? Int {
            pickerViewTime.selectRow(selectedRow, inComponent: 0, animated: true)
        }
        
        // how can i get the output from the PickerView "didSelectRow" in this textfields / label ?
        pickerViewLabel.text = "time"
        timeStd.text = "h"
        timeMin.text = "min"
        timeSec.text = "sec"
    }

}

如何在文本字段和标签中time h min显示值?sec

4

0 回答 0