我的故事板上有一个UITextField,如何将数字返回给我的乐趣。 我的乐趣:
func translet(number:Double,from:String,to:String) -> Double
完整代码:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var unitTypePicker: UIPickerView!
@IBOutlet weak var unitFromPicker: UIPickerView!
@IBOutlet weak var unitToPicker: UIPickerView!
@IBOutlet weak var unitTypeLabel: UILabel!
@IBOutlet weak var unitFromLabel: UILabel!
@IBOutlet weak var unitToLabel: UILabel!
@IBOutlet weak var TrabsletCacl: UILabel!
@IBOutlet weak var textinput: UITextField!
@IBAction func unitTypeBTN(sender: AnyObject) {
unitTypePicker.hidden = false
unitFromPicker.hidden = true
unitToPicker.hidden = true
unitTypePicker.reloadAllComponents()
unitFromLabel.text = ""
unitToLabel.text = ""
}
@IBAction func unitFromBTN(sender: AnyObject) {
unitTypePicker.hidden = true
unitFromPicker.hidden = false
unitToPicker.hidden = true
unitFromPicker.reloadAllComponents()
}
@IBAction func unitToBTN(sender: AnyObject) {
unitTypePicker.hidden = true
unitFromPicker.hidden = true
unitToPicker.hidden = false
unitToPicker.reloadAllComponents()
}
// First Array for Unit Type
let UnitDataArray = ["Unit for Weight","Unit for Length","Unit for Area","Unit for Volume","Unit for Quantity"]
// Sec Array for Unit Type from or to
let UnitforLengthData = ["m","km","cm","mm","yd","ft","in"]
let UnitforWeightData = ["g","kg","m.t","l.t","sh.t","lb"]
let UnitforArea = ["sq.m","sq.ft","sq.yd","sq.in"]
let UnitforVolume = ["L","ml","gal","bu","cu.m","cu.in","cu.in."]
let UnitforQuantity = ["pcs","pkg","pair","set","dozen","gross","ream","roll","unit","carton","bale","barrel","bag"]
override func viewDidLoad() {
super.viewDidLoad()
//textinput.delegate = self
//self.view.addSubview(textinput)
unitTypeLabel.text = "Unit for Weight"
unitTypeLabel.font = UIFont(name: "HelveticaNeue-Thin", size: 30)
unitFromLabel.font = UIFont(name: "HelveticaNeue-Thin", size: 17)
unitToLabel.font = UIFont(name: "HelveticaNeue-Thin", size: 17)
//unitType delegate and datasource
unitTypePicker.delegate = self
unitTypePicker.dataSource = self
//unitFrom delegate and datasource
unitFromPicker.delegate = self
unitFromPicker.dataSource = self
//unitTo delegate and datasource
unitToPicker.delegate = self
unitToPicker.dataSource = self
// pickerView hidden
unitFromPicker.hidden = true
unitToPicker.hidden = true
unitTypePicker.hidden = true
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == unitTypePicker {
return UnitDataArray.count
}else if pickerView == unitFromPicker {
if unitTypeLabel.text == "Unit for Weight" {
return UnitforWeightData.count
}else if unitTypeLabel.text == "Unit for Length" {
return UnitforLengthData.count
}else if unitTypeLabel.text == "Unit for Area" {
return UnitforArea.count
}else if unitTypeLabel.text == "Unit for Volume" {
return UnitforVolume.count
}else{
return UnitforQuantity.count
}
}else{
if unitTypeLabel.text == "Unit for Weight" {
return UnitforWeightData.count
}else if unitTypeLabel.text == "Unit for Length" {
return UnitforLengthData.count
}else if unitTypeLabel.text == "Unit for Area" {
return UnitforArea.count
}else if unitTypeLabel.text == "Unit for Volume" {
return UnitforVolume.count
}else{
return UnitforQuantity.count
}
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == unitTypePicker {
return UnitDataArray[row]
}else if pickerView == unitFromPicker {
if unitTypeLabel.text == "Unit for Weight" {
return UnitforWeightData[row]
}else if unitTypeLabel.text == "Unit for Length" {
return UnitforLengthData[row]
}else if unitTypeLabel.text == "Unit for Area" {
return UnitforArea[row]
}else if unitTypeLabel.text == "Unit for Volume" {
return UnitforVolume[row]
}else{
return UnitforQuantity[row]
}
}else{
if unitTypeLabel.text == "Unit for Weight" {
return UnitforWeightData[row]
}else if unitTypeLabel.text == "Unit for Length" {
return UnitforLengthData[row]
}else if unitTypeLabel.text == "Unit for Area" {
return UnitforArea[row]
}else if unitTypeLabel.text == "Unit for Volume" {
return UnitforVolume[row]
}else{
return UnitforQuantity[row]
}
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == unitTypePicker{
unitTypeLabel.text = UnitDataArray[row]
}else if pickerView == unitFromPicker {
if unitTypeLabel.text == "Unit for Weight"{
unitFromLabel.text = UnitforWeightData[row]
}else if unitTypeLabel.text == "Unit for Length" {
unitFromLabel.text = UnitforLengthData[row]
}else if unitTypeLabel.text == "Unit for Area" {
unitFromLabel.text = UnitforArea[row]
}else if unitTypeLabel.text == "Unit for Volume" {
unitFromLabel.text = UnitforVolume[row]
}else{
unitFromLabel.text = UnitforQuantity[row]
}
}else{
if unitTypeLabel.text == "Unit for Weight"{
unitToLabel.text = UnitforWeightData[row]
}else if unitTypeLabel.text == "Unit for Length" {
unitToLabel.text = UnitforLengthData[row]
}else if unitTypeLabel.text == "Unit for Area" {
unitToLabel.text = UnitforArea[row]
}else if unitTypeLabel.text == "Unit for Volume" {
unitToLabel.text = UnitforVolume[row]
}else{
unitToLabel.text = UnitforQuantity[row]
}
}
}
func translet(number:Double,from:String,to:String) -> Double {
switch from{
case "g":
return number
case "kg":
return number * 1000
default:
return number
}
}
}
答案 0 :(得分:0)
我真的不明白你在问什么(当你只使用其中一个时,为什么你的func有两个字符串参数
也许你想要这样的东西:
translet(number:textinput.text.toInt(),from:"kg",to:"g")