Here is the main screen of the app :
I have successfully linked these pickerView to each other, then I have tried to assign the calculation in converting but not to avail :
ex: mile to killo but I could not find a way to do so. I have tried to use "Switch" still nothing happen. I just need someone can show me how can I convert from a certain unit to another unit through the textFields. For example, if you enter a value in a certain texField the converted result will in the other textField and vise versa.
Here is my Code:
import UIKit
class 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;
}
}
I will give you some tips:
If you are a lazy developer, I found this for you: https://github.com/michalkonturek/MKUnits
Hope this helps.