簡體   English   中英

如何將兩個不同的UIPickerView數據添加到單個標簽中?

[英]How to add two different UIPickerView data into single label?

我有兩個不同的UIPickerViews,如圖所示圖像的左持續時間和右持續時間具有相同的數據,但數組不同 >如何將兩個UIPickerView數據添加到單個標簽中? 如何在單個標簽中添加小時和分鍾?

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
    var hoursSelected1 = ""
    var minsSelected1 = ""

    var hoursSelected2 = ""
    var minsSelected2 = ""

    if strDurationComeFrom == "left"{

        hoursSelected1 = hoursArray1[myPickerView1.selectedRow(inComponent: 0)]
        minsSelected1 = minsArray1[myPickerView1.selectedRow(inComponent: 1)]
        leftDuration.text = "\(hoursSelected1) \(minsSelected1)"

    }

    else{

        hoursSelected2 = hoursArray2[myPickerView2.selectedRow(inComponent: 0)]
         minsSelected2 = minsArray2[myPickerView2.selectedRow(inComponent: 1)]



       rightDuration.text = "\(hoursSelected2) \(minsSelected2)"
    }
    totalDuration = leftDuration.text+rightDuration.text
}

誰能幫我 ?

我要假設hoursArrayminsArray具有整數,如果沒有,只需將它們轉換並在titleForRow 我沒有理由擁有minsArray1minsArray2 分鍾會如何變化? 時間如何不同? 您只能有一個minsArray用作兩個選擇器的數據源。

if component == 0 {
    return "\(hoursArray[titleForRow]) h"
} else {
    return "\(minsArray[titleForRow]) m"
}

然后:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
    let hourSelected = hoursArray[row]
    let minSelected = minsArray[row]
    let otherHour: Int
    let otherMin: Int
    let otherPickerView: UIPickerView!
    let labelText = "\(hourSelected)h \(minSelected)m"

    if pickerView == pickerView1 {
        leftDuration.text = labelText
        otherPickerView = pickerView2
    } else {
        rightDuration.text = labelText
        otherPickerView = pickerView1
    }

    otherHour = hoursArray[otherPickerView.selectedRow(inComponent: 0)]
    otherMin = minsArray[otherPickerView.selectedRow(inComponent: 1)]

    totalDuration = "\(hoursSelected + otherHour)h \(minSelected + otherMin)m"

}

測試此代碼將意味着重現您的整個設置,並且將花費很長時間,但是我確信邏輯可以正常工作。 如果您有任何疑問,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM