如何在UIPickView后面添加“小时”,“分钟”,“秒”文本

问题描述 投票:0回答:1

我正在制作一个具有倒计时器功能的应用程序。

我想像这样显示UIPickView。enter image description here

这是我的应用程序enter image description here

我尝试使用 UIDatePicker 但它不包括秒

我尝试使用 UILabel 添加它们,但在不同设备上运行时它们不正确。

我是一个新编码员,请帮助我!

func displayTimePickerAlert() {let alertController = UIAlertController(title: "Select countdown time", message: "\n\n\n\n\n\n", preferredStyle: .actionSheet)
    timePickerView.dataSource = self
    timePickerView.delegate = self
    timePickerView.frame = CGRect(x: 8, y: 20, width: alertController.view.bounds.width - 20, height: 140)
    alertController.view.addSubview(timePickerView)
    
    
    let okAction = UIAlertAction(title: "OK", style: .default) { \[weak self\] (\_) in
   
        let selectedHour = self?.selectedHour ?? 0
        let selectedMinute = self?.selectedMinute ?? 0
        let selectedSecond = self?.selectedSecond ?? 0
        self?.remainingTime = (selectedHour \* 3600) + (selectedMinute \* 60) + selectedSecond
    }
    alertController.addAction(okAction)
    present(alertController, animated: true, completion: nil)
}

扩展 CountdownController:UIPickerViewDataSource、UIPickerViewDelegate {

func numberOfComponents(in pickerView: UIPickerView) -\> Int {
    return 3
}

func pickerView(\_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -\> Int {
    switch component {
    case 0: // Giờ
        return hours.count
    case 1: // Phút
        return minutes.count
    case 2: // Giây
        return seconds.count
    default:
        return 0
    }
}

func pickerView(\_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -\> String? {
    switch component {
    case 0:
        return String(hours\[row\])
    case 1:
        return String(minutes\[row\])
    case 2:
        return String(seconds\[row\])
    default:
        return ""
    }
}

func pickerView(\_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if component == 0 {
        selectedHour = hours\[row\]
    } else if component == 1 {
        selectedMinute = minutes\[row\]
    } else if component == 2 {
        selectedSecond = seconds\[row\]
    }
    updateCountdownLabel()
}

}

uilabel uipickerview uipickerviewcontroller
1个回答
0
投票

你可以尝试这样定制。

类 TimerSelectViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    pickerView.delegate = self
}

@IBOutlet var pickerView: UIPickerView!

var hour: Int = 0
var minutes: Int = 0
var seconds: Int = 0

}

 extension TimerSelectViewController: UIPickerViewDelegate, UIPickerViewDataSource {

     func numberOfComponents(in pickerView: UIPickerView) -> Int {
         return 3
     }

     func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
         switch component {
         case 0:
             return 25
         case 1, 2:
             return 60
         default:
             return 0
         }
     }

     func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
         return pickerView.frame.size.width/3
     }

     func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
         switch component {
         case 0:
             return "\(row) Hour"
         case 1:
             return "\(row) Minute"
         case 2:
             return "\(row) Second"
         default:
             return ""
         }
     }
     func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
         switch component {
         case 0:
             hour = row
         case 1:
             minutes = row
         case 2:
             seconds = row
         default:
             break;
         }
     }
 }
© www.soinside.com 2019 - 2024. All rights reserved.