UITextfield 作为浮动通过 segue 传递到 UILabel
UITextfield as float passed through segue to an UILabel
我是 swift 的新手,希望用户使用 UITextfield 的小数位输入数字以输入浮点数,然后使用 UILabel 将该浮点数传递给另一个视图控制器。除了将浮点数传递给另一个视图控制器上的 UILabel 之外,一切正常。我得到这个错误
"Cannot assign value of type 'UITextField?' to type 'UILabel?'"
import UIKit
class SelectAttributesViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var kitPickerView: UIPickerView!
@IBOutlet weak var reactionVolumeLabel: UILabel!
@IBOutlet weak var numberOfSampleTextField: UITextField!
@IBOutlet weak var volumeOfTemplateDNATextField: UITextField!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var calculateButton: UIButton!
let kits = ["kit 1", "Kit 2"]
let pArray = [10,20,50]
override func viewDidLoad() {
super.viewDidLoad()
self.numberOfSampleTextField.delegate = self
self.volumeOfTemplateDNATextField.delegate = self
kitPickerView.dataSource = self
kitPickerView.delegate = self
let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))
view.addGestureRecognizer(tap)
}
func pSliderInterval() {
slider.minimumValue = 0
slider.maximumValue = Float(pArray.count)
slider.isContinuous = false
}
@IBAction func reactionVolumeSlider(_ sender: UISlider) {
print(Int(sender.value))
let currentValue = pArray[Int(sender.value)]
reactionVolumeLabel.text = "\(currentValue) "
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return kits.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row == 0{ print(kits[row])
}else if row == 1{
createAlert(title: "Kit not available", message: "More kits will be added soon!")
calculateButton.isEnabled = false
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return kits[row]
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let oldText = textField.text, let r = Range(range, in: oldText) else {
return true
}
let newText = oldText.replacingCharacters(in: r, with: string)
let isNumeric = newText.isEmpty || (Double(newText) != nil)
let numberOfDots = newText.components(separatedBy: ".").count - 1
let numberOfDecimalDigits: Int
if let dotIndex = newText.firstIndex(of: ".") {
numberOfDecimalDigits = newText.distance(from: dotIndex, to: newText.endIndex) - 1
} else {
numberOfDecimalDigits = 0
}
return isNumeric && numberOfDots <= 1 && numberOfDecimalDigits <= 2
}
func createAlert (title: String, message: String){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { (Action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
// This is the Segue.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var masterMix = segue.destination as! MasterMixTableViewController
masterMix.waterVolumeLabel = numberOfSampleTextField
}
@IBAction func calculatePressed(_ sender: Any) {
if numberOfSampleTextField.text != ""{
performSegue(withIdentifier: "masterMixSegue", sender: self)
}
}
}
我浏览了大量的解决方案,但没有找到任何东西。
你需要
let masterMix = segue.destination as! MasterMixTableViewController
masterMix.loadViewIfNeeded()
masterMix.waterVolumeLabel!.text = numberOfSampleTextField!.text
错误提示您正在将 UITextField
分配给 UILabel
。但是你必须将 textFeild.text
分配给 label.text
.
let textField = UITextField()
let label = UILabel()
label.text = textField.text
我是 swift 的新手,希望用户使用 UITextfield 的小数位输入数字以输入浮点数,然后使用 UILabel 将该浮点数传递给另一个视图控制器。除了将浮点数传递给另一个视图控制器上的 UILabel 之外,一切正常。我得到这个错误
"Cannot assign value of type 'UITextField?' to type 'UILabel?'"
import UIKit
class SelectAttributesViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var kitPickerView: UIPickerView!
@IBOutlet weak var reactionVolumeLabel: UILabel!
@IBOutlet weak var numberOfSampleTextField: UITextField!
@IBOutlet weak var volumeOfTemplateDNATextField: UITextField!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var calculateButton: UIButton!
let kits = ["kit 1", "Kit 2"]
let pArray = [10,20,50]
override func viewDidLoad() {
super.viewDidLoad()
self.numberOfSampleTextField.delegate = self
self.volumeOfTemplateDNATextField.delegate = self
kitPickerView.dataSource = self
kitPickerView.delegate = self
let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))
view.addGestureRecognizer(tap)
}
func pSliderInterval() {
slider.minimumValue = 0
slider.maximumValue = Float(pArray.count)
slider.isContinuous = false
}
@IBAction func reactionVolumeSlider(_ sender: UISlider) {
print(Int(sender.value))
let currentValue = pArray[Int(sender.value)]
reactionVolumeLabel.text = "\(currentValue) "
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return kits.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row == 0{ print(kits[row])
}else if row == 1{
createAlert(title: "Kit not available", message: "More kits will be added soon!")
calculateButton.isEnabled = false
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return kits[row]
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let oldText = textField.text, let r = Range(range, in: oldText) else {
return true
}
let newText = oldText.replacingCharacters(in: r, with: string)
let isNumeric = newText.isEmpty || (Double(newText) != nil)
let numberOfDots = newText.components(separatedBy: ".").count - 1
let numberOfDecimalDigits: Int
if let dotIndex = newText.firstIndex(of: ".") {
numberOfDecimalDigits = newText.distance(from: dotIndex, to: newText.endIndex) - 1
} else {
numberOfDecimalDigits = 0
}
return isNumeric && numberOfDots <= 1 && numberOfDecimalDigits <= 2
}
func createAlert (title: String, message: String){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { (Action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
// This is the Segue.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var masterMix = segue.destination as! MasterMixTableViewController
masterMix.waterVolumeLabel = numberOfSampleTextField
}
@IBAction func calculatePressed(_ sender: Any) {
if numberOfSampleTextField.text != ""{
performSegue(withIdentifier: "masterMixSegue", sender: self)
}
}
}
我浏览了大量的解决方案,但没有找到任何东西。
你需要
let masterMix = segue.destination as! MasterMixTableViewController
masterMix.loadViewIfNeeded()
masterMix.waterVolumeLabel!.text = numberOfSampleTextField!.text
错误提示您正在将 UITextField
分配给 UILabel
。但是你必须将 textFeild.text
分配给 label.text
.
let textField = UITextField()
let label = UILabel()
label.text = textField.text