Swift 如何在类似Android的警报控制器中编写快速数字选择器
如何使用变量定义的最小值和最大值创建数字选择器 在我的Android应用程序中,我创建了一个带有选择器编号的AlertDialog,最大编号链接到数据库中的一个值 是否有可能在Swift中做同样的事情,或者做一些类似的事情 我的警报控制器代码是:Swift 如何在类似Android的警报控制器中编写快速数字选择器,swift,xcode,uialertcontroller,numberpicker,Swift,Xcode,Uialertcontroller,Numberpicker,如何使用变量定义的最小值和最大值创建数字选择器 在我的Android应用程序中,我创建了一个带有选择器编号的AlertDialog,最大编号链接到数据库中的一个值 是否有可能在Swift中做同样的事情,或者做一些类似的事情 我的警报控制器代码是: func createAlertCarte1(Title:String, Message:String) { let alertCarte1 = UIAlertController(title: Title, message: Mess
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)
}
这回答了你的问题吗@rbaldwin是的,这对我很有用,但我主要是想用一个变量来定义NumberPicker的最大数量。这能回答你的问题吗@rbaldwin是的,这对我很有用,但我主要是想用一个变量来定义NumberPicker的最大数量。哦,非常感谢,我没有要求这么多!:但是我还有最后一个问题,关于这个消息:什么是新行?\n是新行-通过添加空行为选择器创建一些空间是一种黑客行为。也许有更好的方法。哦,非常感谢,我没有要求这么多!:但是我还有最后一个问题,关于这个消息:什么是新行?\n是新行-通过添加空行为选择器创建一些空间是一种黑客行为。也许有更好的办法。
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var pickerView: UIPickerView!
var pickerData: [Int]!
override func viewDidLoad() {
super.viewDidLoad()
pickerView = UIPickerView(frame: CGRect(x: 10, 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
let pickerValue = self.pickerData[self.pickerView.selectedRow(inComponent: 0)]
print("Picker value: \(pickerValue) was selected")
}))
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
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])"
}
}