import Foundation
class Balkenbewehrung : UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var multiplicator : Double = 1.0
var pickerDataSource = ["Ø 6",
"Ø 8",
"Ø 10",
"Ø 12",
"Ø 14",
"Ø 16",
"Ø 20",
"Ø 25",
"Ø 26",
"Ø 28",
"Ø 30",
"Ø 32",
"Ø 36",
"Ø 40",
"Ø 50"]
@IBOutlet var menü: UIBarButtonItem!
@IBOutlet weak var myPicker: UIPickerView!
@IBOutlet weak var myLabel: UILabel!
@IBOutlet var txt: UITextField!
@IBOutlet var ausgabe: UILabel!
@IBAction func b(sender: AnyObject) {
let result = multiplicator * (txt.text as NSString).doubleValue
ausgabe.text = "\(result)"
}
override func viewDidLoad() {
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
menü.target = self.revealViewController()
menü.action = Selector("revealToggle:")
myPicker?.delegate = self
myPicker?.dataSource = self
myPicker.layer.borderColor = UIColor.darkGrayColor().CGColor
myPicker.backgroundColor = UIColor.whiteColor()
self.view.backgroundColor = UIColor.lightGrayColor()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerDataSource.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerDataSource[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if(row==0)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==1)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==2)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==3)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==4)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==5)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==6)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==7)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==8)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==9)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==10)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==11)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==12)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==13)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
else if(row==14)
{
myLabel.text = "0.478"
multiplicator = 0.478
}
}
}
答案 0 :(得分:0)
尝试这样的事情,假设有两个UILabel
wert1
和wert2
。
wert1
为空,请将字符串放在wert1
wert2
为空,请将字符串放在wert2
如果没有标签为空,请执行操作。
@IBAction func b(sender: AnyObject) {
let result = multiplicator * (txt.text as NSString).doubleValue
let resultString = "\(result)"
ausgabe.text = resultString
if wert1.text.isEmpty {
wert1.text = resultString
} else if wert2.text.isEmpty {
wert2.text = resultString
} else {
doMultiplication()
}
}
您可以使用乘数值数组和pickerView:didSelectRow:
来简化NSNumberFormatter
方法以获取本地化字符串
let multiplicators = [0.478, 0.478, 0.478, 0.478, 0.478, 0.478, 0.478, 0.478, 0.478, 0.478, 0.478, 0.478, 0.478, 0.478]
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
multiplicator = multiplicators[row]
let multiplicatorString = NSNumberFormatter.localizedStringFromNumber(NSNumber(double: multiplicator), numberStyle: .DecimalStyle)
myLabel.text = multiplicatorString
}