2017-01-10 1 views
0

Voici l'écran principal de l'application:Création TROIS pickerViews pour convertir les unités

enter image description here

J'ai réussi à lier ces pickerView à l'autre, alors je l'ai essayé d'attribuer le calcul dans la conversion, mais pas pour bénéficier:

ex: mile to killo mais je ne pouvais pas trouver un moyen de le faire. J'ai essayé d'utiliser "Switch" encore rien ne se passe. J'ai juste besoin que quelqu'un puisse me montrer comment je peux convertir d'une certaine unité à une autre unité à travers les textFields. Par exemple, si vous entrez une valeur dans un certain champ texField, le résultat converti sera dans l'autre textField et vice versa.

Voici mon code:

import UIKit 

classe ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {

@IBOutlet weak var mainPicker: UIPickerView! 
@IBOutlet weak var leftPicker: UIPickerView! 
@IBOutlet weak var rightPicker: UIPickerView! 
@IBOutlet weak var textFieldLeft: UITextField! 
@IBOutlet weak var textFielfRight: UITextField! 
@IBOutlet weak var equal: UILabel! 


var leftPickerData : [String] = [] 
var rightPickerData : [String] = [] 

var dataDict:NSMutableDictionary! 
var mainPickerData:NSArray! 
var leftRightPickerData:NSArray! 

//yourPicker.backgroundColor = UIColor(patternImage: UIImage(named: "back.jpg")!) 


override func viewDidLoad() { 
    super.viewDidLoad() 

    mainPicker.backgroundColor = .clear 
    rightPicker.backgroundColor = .clear 
    leftPicker.backgroundColor = .clear 

    // Connect data to ViewController .. 
    self.mainPicker.delegate = self 
    self.mainPicker.dataSource = self 

    self.leftPicker.delegate = self 
    self.leftPicker.dataSource = self 

    self.rightPicker.delegate = self 
    self.rightPicker.dataSource = self 

    self.textFieldLeft.delegate = self 
    self.textFielfRight.delegate = self 

    let theWidth = view.frame.size.width 
    let theHeight = view.frame.size.height 

    mainPicker.center = CGPoint(x: theWidth/2, y: theHeight/2 - 182.5) 
    leftPicker.center = CGPoint(x: theWidth/2 - 100, y: theHeight/2) 
    rightPicker.center = CGPoint(x: theWidth/2 + 100, y: theHeight/2) 
    textFieldLeft.center = CGPoint(x: theWidth/2 - 90, y: theHeight/2 + 110) 
    textFielfRight.center = CGPoint(x: theWidth/2 + 90, y: theHeight/2 + 110) 
    equal.center = CGPoint(x: theWidth/2, y: theHeight/2 + 110) 



    dataDict = ["Area":["Square Mile", "Square Yard", "Square Foot", "Square Inch", "Hectare", "Acre", "Square Kilometer", "Square Meter", "Square Centimeter", " Square Millimeter"] 
     ,"Energy":["Btus", "Calories", "Ergs", "Foot-Pounds", "Joules", "Kilogram-Calories", "Kilogram-Meters", "Kilowatt-Hours", "Newton-Meters", "Watt-Hours"], "Length":["Mile", "Yard", "Foot", "Inch", "Kilometer", "Meter", "Centimeter", "Millimeter"], "Power": ["Btus/Minute", "Foot-Pounds/Min", "Foot-Pounds/Sec", "Horsepower", "Kilowatts", "Watts"], "Pressure": ["Pounds/Sqr Ft", "Pounds/Sqr In", "Atmospheres", "Bars", "In of Mercury", "Cm of Mercury", "Kilograms/Sqr Meter", "Pascals"], "Speed": ["Knots", "Miles/Hr", "Miles/Min", "Feet/Min", "Feet/Sec", "Kilometers/Hr", "Kilometer/Min", "Meters/Sec"], "Temperature": ["Celsius C˚", "Fahrenheit", "Kelvin"], "Time": ["Years", "Months", "Weeks", "Days", "Hours", "Minutes", "Seconds", "Millisconds", "Microseconds", " Nanoseconds"], "Volume": ["Cupic Feet","Cubic Meter", "Gallon (Imp)", "Gallon (US)", "Quart (US)", "Pint (US)", "Fluid Oz", "Cup", "Tablespoon", "Teaspoon", "Dram (US)", "Liter"], "Weight": ["Short Ton (US)","Long Ton (UK)", "Pound (U.S)", "Ounce (US)", "Stone", "Metric Ton", "Kilogram", "Gram"]] 
    mainPickerData = dataDict.allKeys as NSArray!; 
    leftRightPickerData = dataDict.object(forKey: mainPickerData.firstObject as! String) as! NSArray 

    // Linking the textFields with the pickerViews. 
    //textFieldLeft.inputView = leftPicker; 
    // textFielfRight.inputView = rightPicker; 



    } 



// The number of columns of data 
func numberOfComponents(in pickerView: UIPickerView) -> Int { 
    return 1 
} 

// The number of rows of data 
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 

    switch (pickerView.tag) { 

    case mainPicker.tag: 

     return mainPickerData.count 

    case leftPicker.tag,rightPicker.tag: 

     let currentSelectedIndex = mainPicker.selectedRow(inComponent: component) 
     leftRightPickerData = (dataDict.object(forKey: mainPickerData[currentSelectedIndex] as! String) as! NSArray) 

     return leftRightPickerData.count; 

    default: 
     break; 
    } 
    return 0; 
} 

// The data to return for the row and component (column) that's being passed in 
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 

    if leftPicker.tag == 2 { 

     return leftPickerData[row] 

    }else if rightPicker.tag == 3{ 

     return rightPickerData[row] 

    } 

    return "" 
} 

// Catpure the picker view selection 
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    // This method is triggered whenever the user makes a change to the picker selection. 
    // The parameter named row and component represents what was selected. 

    if(pickerView.tag == 1){ 

     let currentSelectedIndex = mainPicker.selectedRow(inComponent: component) 
     leftRightPickerData = (dataDict.object(forKey: mainPickerData[currentSelectedIndex] as! String) as! NSArray) 

     leftPicker.reloadAllComponents() 
     rightPicker.reloadAllComponents() 

     if mainPicker.tag == mainPicker.selectedRow(inComponent: component) { 

      if leftPicker.tag == leftPicker.selectedRow(inComponent: component) && rightPicker.tag == rightPicker.selectedRow(inComponent: component){ 

       textFieldLeft.text = textFielfRight.text 
      } 
     } 

    } 



} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

    self.view.endEditing(true) 

} 

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 

    textFieldLeft.resignFirstResponder() 
    textFielfRight.resignFirstResponder() 

    return true 

} 

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 

    let titleData : String? 
    if(pickerView.tag == mainPicker.tag){ 
     titleData = mainPickerData[row] as? String; 

    } 
    else{ 

     let currentSelectedIndex = mainPicker.selectedRow(inComponent: 0) 
     leftRightPickerData = (dataDict.object(forKey: mainPickerData[currentSelectedIndex] as! String) as! NSArray) 

     titleData = leftRightPickerData[row] as? String; 
    } 

    let myTitle = NSAttributedString(string: titleData!, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 12.0)!,NSForegroundColorAttributeName:UIColor.blue]) 
    return myTitle; 

} 

}

Répondre

0

Je vais vous donner quelques conseils:

  1. Valeur dans un champ de texte est une chaîne, vous devez le convertir en double et ensuite multiplier par une valeur différente entre 2 unités. Pour sauvegarder ces différentes valeurs, j'ai une idée: save [1, 1760, 5280, 63360, 1.609344, 1609.344, 160934.4, 1609344] pour ["Mile", "Yard", "Pied", "Pouce", "Kilomètre", "Compteur", "Centimètre", "Millimètre"] puis faire des calculs mathématiques.
  2. Vous devriez écouter la notification UITextFieldTextDidChange pour chaque UITextField pour calculer instantanément la valeur égale.

Si vous êtes un développeur paresseux, je trouve cela pour vous: https://github.com/michalkonturek/MKUnits

Hope this helps.

0

je ferais quelque chose comme cela comme un exemple pour les valeurs de longueur différentes:

Vous souhaitez stocker une sorte de tableau qui suit les valeurs de conversion pour chaque unité de mesure différente, la meilleure unité de longueur universelle est mètres.Ce serait une bonne idée de vous assurer que la valeur correspondante pour chaque unité reste statique, créer un enum:

enum Length: Int { 
    case mile   = 0 
    case yard   = 1 
    case foot   = 2 
    case inch   = 3 
    case kilometer  = 4 
    // ... keep going with all lenght units of measurement 
} 

Ensuite, vous devez faire de votre tableau de valeurs pour les conversions, c'est un tableau double est basé sur la valeurs de longueur I qui figure dans le enum ci-dessus pour la conversion en meters:

// Store values with corresponding indicies in array to the Length enum's values 
let meterConversions:[Double] = [1609.34, 0.9144, 0.3048, 0.0254, 1,000] 

// Store values of length unit descriptions to print after conversion in TextField, long hand or short hand whatever you prefer. (this is optional) 
let lengthUnits = ["Mile", "Yard", "Foot", "Inch", "Kilometer"] 

Ensuite, créez des méthodes de conversion:

// Convert length type to meters 
func convertToMeters(type: Length, unitValue: Double) -> Double { 
    return (meterConversions[type.rawValue] * unitValue) 
} 

// Convert meters back to length type 
func convertFromMeters(type: Length, meterValue: Double) -> Double { 
    return meterValue/meterConversions[type.rawValue] 
} 

// Convert from length type to other length type 
func convertType(from: Length, to: Length, unitValue: Double) -> Double { 

    // Convert from value to meters to start 
    let fromValueToMeters:Double = convertToMeters(type: from, unitValue: unitValue) 

    // Now use that value to convert back to desired unit 
    let newUnitValue:Double = convertFromMeters(type: to, meterValue: fromValueToMeters) 

    return newUnitValue 
} 

Ensuite, chaque fois que l'utilisateur sélectionne une nouvelle ligne dans la gauche ou à droite UIPickerView, mettez à jour le calcul et la poignée comme vous le souhaitez:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

    // Make sure there is actually some text in the left text field, otherwise return 
    guard let text = textFieldLeft.text else { print("textFieldLeft doesn't contain any text"); return } 

    // You are going to need a numerical (Double) value from the user's text 
    let stringAsDouble = Double(text) 

    // Now check that the text was actually a numerical value able to be converted to a double 
    if let value = stringAsDouble { 

     var typeA:Length! 
     var typeB:Length! 

     if pickerView == leftPicker { 
      typeA = Length(rawValue: row)! 
      typeB = Length(rawValue: rightPicker.selectedRow(inComponent: 0))! 
      converted = convertType(from: typeA, to: typeB, unitValue: value) 
     } 
     else if pickerView == rightPicker { 
      typeA = Length(rawValue: leftPicker.selectedRow(inComponent: 0))! 
      typeB = Length(rawValue: row)! 
      let value:Double = 0 // Determine user entered value from textField 
      converted = convertType(from: typeA, to: typeB, unitValue: value) 
     } 

     updateValueAfterConversion(originalValue: value, originalType: typeA, convertedValue: converted, convertedType: typeB) 
    } else { 
     print("Couldn't convert text to double value") 
    } 
} 

func updateValueAfterConversion(originalValue: Double, originalType: Length, convertedValue: Double, convertedType: Length) { 
    // Update text in both fields, lengthUnits part is optional if you want to print unit along with value. 
    // Update text on left side 
    textFieldLeft.text = "\(originalValue) \(lengthUnits[originalType.rawValue])" 
    // Update text on right side 
    textFieldRight.text = "\(convertedValue) \(lengthUnits[convertedType.rawValue])" 
} 
+0

Qu'est-ce que cela signifie pour vous votre dernier commentaire: // Maintenant, faites ce vous voulez avec la valeur convertie –

+0

@Turki M. Alsayed - Alors vous auriez la valeur entrée convertie du type d'unité dans pickerA au type d'unité dans pickerB. Alors disons que l'utilisateur a demandé une conversion de 10 pieds en pouces. Votre variable 'converted' devrait avoir une valeur de' 120.0' - vous pourriez alors mettre à jour le 'UITextField' ou n'importe quel label que vous voulez utiliser pour mettre à jour l'interface utilisateur pour alerter l'utilisateur quant à la valeur convertie. Par exemple: 'textField.text =" Valeur convertie: \ (converti) "' ou s'il s'agit d'une étiquette: 'label.text =" Valeur convertie: \ (converti) "', etc ... – Pierce

+0

Bonjour M. Pierce, J'ai fait exactement ce que vous avez expliqué ci-dessus, j'ai fait seulement pour "LONGUEUR" pour m'assurer que je suis sur le bon chemin. Malheureusement, quand je choisis quelque chose de subLength ex: inch, j'ai "Nan" dans le champ de texte à gauche et à droite. J'ai fait ce qui suit: Votre coopération est très appréciée. si (textFieldLeft.text? .isEmpty)! { textFieldLeft.text = "\ (converti)" } else { textFielfRight.text = "\ (converti)" } –