So, here's a way to implement what you want. You associate the price for the topping with the tag
property of a UISwitch
. Then, you can simply iterate an array of the UISwitch
objects you've got, quickly summing up the cost of the selected toppings, no if
statements needed:
// ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad () {
super.viewDidLoad()
setupUserInterface()
}
var switches: [UISwitch] = [UISwitch]()
var toppingNames = ["Mushrooms", "Cheese", "Pepperoni", "Sausage", "Jalapeño", "Pineapple", "Olive", "Ham", "Bacon"]
var toppingCosts = [10, 20, 50, 45, 40, 30, 60, 75, 100]
var costLabel: UILabel = UILabel()
func switchFlipped () {
var cost: CGFloat = costForSelectedToppings()
var formatter: NSNumberFormatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
var costString: String = formatter.stringFromNumber((cost))!
self.costLabel.text = "Cost for toppings: " + costString
}
func costForSelectedToppings () -> CGFloat {
var cost: CGFloat = 0.0
for swtch: UISwitch in self.switches {
if (swtch.on == true) {
cost = cost + (CGFloat)(swtch.tag)
}
}
return cost / 100.0
}
func setupUserInterface () {
var previousLabel: UILabel?
for i in 0...8 {
var label: UILabel = UILabel()
var swtch: UISwitch = UISwitch()
self.switches.append(swtch)
label.text = self.toppingNames[i]
swtch.tag = self.toppingCosts[i]
label.setTranslatesAutoresizingMaskIntoConstraints(false)
swtch.setTranslatesAutoresizingMaskIntoConstraints(false)
swtch.addTarget(self, action: "switchFlipped", forControlEvents: .ValueChanged)
self.view.addSubview(label)
self.view.addSubview(swtch)
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-[label]-[switch]-|",
options:NSLayoutFormatOptions(0),
metrics:nil,
views:["label" : label, "switch" : swtch]))
self.view.addConstraint(NSLayoutConstraint(
item: swtch,
attribute:.CenterY,
relatedBy:.Equal,
toItem:label,
attribute:.CenterY,
multiplier:1.0,
constant:0.0))
if let prev = previousLabel {
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[label1]-(spacing)-[label2]",
options:NSLayoutFormatOptions(0),
metrics:["spacing" : 16],
views:["label1" : previousLabel!, "label2": label]))
} else {
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-(margin)-[label]",
options:NSLayoutFormatOptions(0),
metrics:["margin" : 40],
views:["label" : label]))
}
previousLabel = label
}
self.costLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
self.costLabel.text = "Cost for toppings: $0.00"
self.view.addSubview(self.costLabel)
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[label]-|",
options:NSLayoutFormatOptions(0),
metrics:nil,
views:["label" : self.costLabel]))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[label1]-(spacing)-[label2]",
options:NSLayoutFormatOptions(0),
metrics:["spacing" : 16],
views:["label1" : previousLabel!, "label2": self.costLabel]))
}
}
Note: Obviously, you'll probably want to update this UI layout. I'm guessing you'll eventually want to place it either in a UITableView
or at least into a UIScrollView
in case the contents run off-screen.