Case statement to change UILabel depending on UISlider value - ios

So, straight in to it. I wish to change the value of a label depending on the position of the UISlider. If the slider is in position 1, and untouched, the label should show "Regular", at value 2 it should show "Soy" and value 3, "Almond".
In case you hadn't guessed already, it is to make a milk choice.
Ideally I wish to use a switch/case. The slider has a minimum value of 1 which refers to regular milk. The aim for the app is to use the cloud down the line to save order information, so I am open as to which would be the best solution complying to this future requirement.
Thanks!
Currently....
#IBOutlet weak var sugarValue: UILabel!
#IBOutlet weak var sugarStepper: UIStepper!
#IBOutlet weak var milkChoice: UILabel!
#IBOutlet weak var milkChoiceSlider: UISlider!
#IBAction func sugarChange(sender: UIStepper) {
sugarValue.text = Double(sugarStepper.value).description
}
#IBAction func milkChoiceChange(sender: UISlider) {
milkChoice.text = String(milkChoiceSlider.value)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
I'm aware that the current set up would not show the text in the label, I was trying to just get it to show the 1, 2, 3 values first but to no avail.

Here is a code snippet... As you see, you can use an array of values, in this case you don't need "case" :)
import UIKit
class ViewController: UIViewController {
#IBOutlet var milkChoiceLabel: UILabel!
#IBOutlet var milkChoiceSlider: UISlider!
#IBOutlet var sugarValue: UILabel!
var labelValues: [String] = ["Regular", "Soy", "Almond"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func sliderValueChanged(sender: UISlider) {
sender.value = roundf(sender.value)
let value = Int(sender.value)
sugarValue.text = value.description
// switch (sender.value) {
// case 1:
// milkChoiceLabel.text = labelValues[0]
// break
// case 2:
// milkChoiceLabel.text = labelValues[1]
// break
// case 3:
// milkChoiceLabel.text = labelValues[2]
// break
// default:
// break
// }
// or better ...
milkChoiceLabel.text = labelValues[value - 1];
}
}

The real problem here is that you're misusing a UISlider as a tri-state switch. A slider is continuous; it's not a switch. If you wanted a tri-state switch, you should use a tri-state switch, namely a UISegmentedControl.

Related

How to get integer values from text fields in Swift?

I want to create a simple BMI calculator using height and weight and I am having trouble converting my UITextField strings to integers for the calculation.
Here's my working code:
import UIKit
class BMICalculator: UIViewController {
//MARK: Properties
#IBOutlet weak var weightField: UITextField!
#IBOutlet weak var heightField: UITextField!
#IBOutlet weak var solutionTextField: UILabel!
#IBAction func calcButton(_ sender: AnyObject) {
let weightInt = Int(weightField)
let heightInt = Int(heightField)
solutionTextField.text = weightInt/(heightInt*heightInt)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Anyone have any ideas? I tried searching for the solution but couldn't find anything specific to this issue.
Use this:
guard let text1 = weightField.text else {
return
}
guard let text2 = heightField.text else {
return
}
guard let weightInt = Int(text1) else {
return
}
guard let heightInt = Int(text2) else {
return
}
solutionTextField.text = weightInt /(heightInt*heightInt)
//Change your name for this outlet 'solutionTextField' to 'solutionLabel' since it is a UILabel not UITextField
The TextField only accepts a String, it wont take an Int.
Change this:
solutionTextField.text = weightInt/(heightInt*heightInt)
To this:
solutionTextField.text = String(weightInt/(heightInt*heightInt))
I don't think your code is working. To get the values out of your UITextFields and convert them to Ints, you'll need to pull them out of the '.text properties. Then, when you calculate the result, you'll need to convert it back to a string and set solutionTextField?.text equal to that result.
class BMICalculator: UIViewController {
//MARK: Properties
#IBOutlet weak var weightField: UITextField!
#IBOutlet weak var heightField: UITextField!
#IBOutlet weak var solutionTextField: UILabel!
#IBAction func calcButton(_ sender: AnyObject) {
let weightInt = Int((weightField?.text!)!)
let heightInt = Int((heightField?.text!)!)
let solution = weightInt!/(heightInt!*heightInt!)
solutionTextField?.text = "\(solution)"
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Keep in mind that this code is very dangerous because you're not safely unwrapping optionals, but that's a different thread.
Hope this helps.

Simple, algorithm that buy something in Swift Coding

I want Subtract process with Button action but my code does not work.
I made the Number. This number is Main number for user's game money. I set the '1,000'. This I set the name of this "playerMoney"
After that I made the #IBAction Button for Discount the '500' number from 'playerMoney'. This button's action means buy Game item things.
I set the name of '500' number is "chocolatePrice" and Button's name is "buyChocolateButton"
I also print playerMoney on the UILable.
I set that label name is "printPlayerMoney"
I'm using the code below.
import UIKit
class ViewController: UIViewController {
var playerMoney = 1000
var chocolatePrice = 500
#IBOutlet weak var printPlayerMoney: UILabel!
#IBAction func buyChocolateButton(sender: AnyObject) {
playerMoney = playerMoney - chocolatePrice
}
override func viewDidLoad() {
super.viewDidLoad()
printPlayerMoney.text = "\(playerMoney)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
You need to put the printPlayerMoney.text = "\(playerMoney)" inside the button function. That way, overtime you press the button, it does the calculation and then updates the label. See the updated code below:
class ViewController: UIViewController {
var playerMoney: Int = 1000
var chocolatePrice: Int = 500
#IBOutlet weak var printPlayerMoney: UILabel!
#IBAction func buyChocolateButton(sender: AnyObject) {
playerMoney = playerMoney - chocolatePrice
printPlayerMoney.text = "\(playerMoney)"
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
printPlayerMoney.text = "\(playerMoney)"
doesn't watch the value of playerMoney
After changing the value of playerMoney, we have to set the text of the UILabel again if we're expecting a visual change

Add the value of multiple sliders in swift then average them?

Im doing this wrong I but I'm not sure how to get it I could really use some help. I have 3 sliders (I may need more later) and basically they are 1-10 in value kinda for a rating system and i want them averaged to create an average score.. so this is what I have
#IBOutlet weak var speed: UISlider!
#IBAction func speedChange(sender: AnyObject) {
let speedValue = speed.value
}
#IBOutlet weak var body: UISlider!
#IBAction func bodyChange(sender: AnyObject) {
let bodyValue = body.value
}
#IBOutlet weak var details: UISlider!
#IBAction func detailsChange(sender: AnyObject) {
let detailsValue = details.value
}
#IBOutlet weak var score: UILabel!
func final() {
var thescore = (speed.value + body.value + details.value) / 3
score.text = "\(thescore)"
}
Only problem is it doesn't work. the + errors out, so I need to fix that, but even still i think my methodology is completely off.
It worked for me. Are you sure the outlets in the storyboard are linked to your Controller ?
Also, you may want to call your function final every time a slider changed its value, like this :
#IBOutlet weak var sliderA: UISlider!
#IBAction func sliderAchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var sliderB: UISlider!
#IBAction func sliderBchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var sliderC: UISlider!
#IBAction func sliderCchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
fonction()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func fonction() {
var thescore = (sliderA.value + sliderB.value + sliderC.value ) / 3
label.text = "\(thescore)"
}​
And I added a call to the function in viewDidLoad method to initilaize the label's text.

How to make an array of UILabel in Swift

How do I make an array of UILabel in Swift. When I try to, I get an error like ViewController.Type'dose not have a member named 'Lable00'
my code:
import UIKit
class ViewController: UIViewController {
let individualScores = [75, 43, 103, 87, 12]
#IBOutlet var Lable00: UILabel?
#IBOutlet var Lable01: UILabel?
#IBOutlet var Lable02: UILabel?
#IBOutlet var Lable03: UILabel?
#IBOutlet var Lable04: UILabel?
var Lable_Arr = [Lable00, Lable01, Lable02, Lable03, Lable04]
override func viewDidLoad() {
super.viewDidLoad()
for score in individualScores {
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
private var labels:[UILabel]()
override func viewDidLoad()
{
super.viewDidLoad()
labels.append(Lable00)
labels.append(Lable01)
labels.append(Lable02)
labels.append(Lable03)
labels.append(Lable04)
// do stuff with the labels view
}
for Swift 2 :
var labels:[UILabel]=[]
In ViewDid load you don't have to append, you can just assign a new array to labels like so:
var labels:[UILabel]()
override func viewDidLoad() {
super.viewDidLoad()
labels = [label01, label02, label03, label04]
// do stuff with the labels view
}

How do you get a UI button to do different tasks each time it is pressed?

When I create an IBaction for a button I can easily get it to perform an action for example:
#IBAction func click(sender: AnyObject) {
label6.hidden = false}
How do I get it to execute a totally different task when it is clicked for a second or third time etc?
Thanks
EDIT: When I try this I get an error "Viewcontroller.type does not have a member named "label1", this also is stated for "label2" Any ideas why as I have already added the label in as an outlet. Do I need to declare it somewhere else to get it to work? Thanks
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
label1.text = ""
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
class MyButtonClass
{
var clickCounter: Int = 0
#IBAction func click(sender: AnyObject)
{
clickCounter += 1
if (clickCounter == 1)
{label1.text = "text1"}
else if (clickCounter == 2)
{label2.text = "text1"
clickCounter = 0}
}
}
}
Replace your code with the following:
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
label1.text = ""
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
var clickCounter: Int = 0
#IBAction func click(sender: AnyObject)
{
clickCounter += 1
if (clickCounter == 1)
{
label1.text = "text1"
}
else if (clickCounter == 2)
{
label2.text = "text1"
clickCounter = 0
}
}
}
You can have a counter and depending on its value fire another method from the one called on click
Create a property that counts the tap count
Increment the tap count on each click
Implement a switch on the tap count to perform different actions (IBAction acts as a dispatcher)

Resources