您知道一种用变量定义的最大值和最小值创建数字选择器的方法吗?
在我的Android应用程序上,我创建了一个带有Picker编号的AlertDialog,并且最大数量链接到数据库中的值。是否可以在Swift中做同样的事情,或者做一些接近它的事情?
我的警报控制器代码:
func createAlertCarte1(Title:String, Message:String) {
let alertCarte1 = UIAlertController(title: Title, message: Message, preferredStyle: UIAlertController.Style.alert)
alertCarte1.addAction(UIAlertAction.init(title: "Ok", style: UIAlertAction.Style.default, handler: { (action1) in
alertCarte1.dismiss(animated: true, completion: nil)
self.displayAd()
}))
alertCarte1.addAction(UIAlertAction.init(title: "Annuler", style: UIAlertAction.Style.cancel, handler: { (action1) in
alertCarte1.dismiss(animated: true, completion: nil)
}))
self.present(alertCarte1, animated: true, completion: nil)
}
先谢谢您。
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var pickerView: UIPickerView!
var pickerData: [Int]!
override func viewDidLoad() {
super.viewDidLoad()
pickerView = UIPickerView(frame: CGRect(x: 0, y: 50, width: 250, height: 150))
pickerView.delegate = self
pickerView.dataSource = self
// This is where you can set your min/max values
let minNum = 1
let maxNum = 10
pickerData = Array(stride(from: minNum, to: maxNum + 1, by: 1))
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
showAlert()
}
func showAlert() {
let ac = UIAlertController(title: "Picker", message: "\n\n\n\n\n\n\n\n\n\n", preferredStyle: .alert)
ac.view.addSubview(pickerView)
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
// code
}))
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
// code
}))
present(ac, animated: true)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "\(pickerData[row])"
}
}