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

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.

Related

How do I change what property to use in function of action based off of sender in Swift 4

#IBOutlet weak var weekdaysHoursSliderOutlet: UISlider!
#IBOutlet weak var weekdaysHoursLabelOutlet: UILabel!
#IBOutlet weak var weekdaysHoursNumberLabel: UILabel!
#IBOutlet weak var weekdendsHoursSliderOutlet: UISlider!
#IBOutlet weak var weekendsHoursLabelOutlet: UILabel!
#IBOutlet weak var weekendsHoursNumberLabel: UILabel!
#IBAction func weekendsHoursSliderChange(_ sender: UISlider) {
let currentValue: Int = Int(sender.value)
weekendsHoursNumberLabel.text = ("\(currentValue)")
if(currentValue == 1){
weekendsHoursLabelOutlet.text = ("Hour Per Day")
}
else{
weekendsHoursLabelOutlet.text = ("Hours Per Day")
}
}
I would want to change that to weekdays if say the weekdaysHourSliderOutlet called an on change Action. I plan on implementing more than just weekends and weekdays so I don't want a bunch of redundant code. I was thinking of using a dictionary of some sort but couldn't get it hooked up correctly. Thank you in advance!
func viewDidLoad() {
weekdaysHoursSliderOutlet.tag = 1
weekdendsHoursSliderOutlet.tag = 2
}
#IBAction func sliderChanged(_ sender: UISlider) {
if sender.tag == 1 {
//handle weekdaysHoursSliderOutlet
}
else {
//handle weekdendsHoursSliderOutlet
}
}

Geting a fatal "error: unexpectedly found nil while unwrapping an Optional value" when chaging value of label on button press

I keep getting nil value when I press button.
import UIKit
class ViewController: UIViewController {
//MARK: Properties
#IBOutlet weak var txt_vrednost: UITextField!
#IBOutlet weak var btn_dodaj: UIButton!
#IBOutlet weak var lbl_stanje: UILabel!
var novcanik = 0.0
//MARK: Acttion
#IBAction func btn_dodaj_pressed(_ sender: Any) {
let values = Double(txt_vrednost.text!)
novcanik = novcanik + values!
lbl_stanje.text=String(novcanik)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
First of all make sure all your outlets are connected with storyboard. You are using force unwrap everywhere, which is causing crash :
import UIKit
class ViewController: UIViewController {
//MARK: Properties
#IBOutlet weak var txt_vrednost: UITextField!
#IBOutlet weak var btn_dodaj: UIButton!
#IBOutlet weak var lbl_stanje: UILabel!
var novcanik = 0.0
//MARK: Action
#IBAction func btn_dodaj_pressed(_ sender: Any) {
guard let values = txt_vrednost.text as? Double else {
print("not convertible")
return
}
novcanik = novcanik + values
lbl_stanje.text = "\(novcanik)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}

Swift Thread 1:EXC_BREAKPOINT error

I'm a newbie to swift and for now it's a pain in the a$$ to code.
I'm trying to make a very simple app where the user enters a number to a textfield
and then when a button is clicked it shows the number + 1 in a label.
I always get this error:
Thread 1:EXC_BREAKPOINT (code=1, subcode=0x1002c11ec)
This is my code:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tav: UITextField!
#IBOutlet weak var fogy: UITextField!
#IBOutlet weak var ar: UITextField!
#IBOutlet weak var eredmeny: UILabel!
#IBOutlet weak var szamolasBtn: UIButton!
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 szamol(_ sender: Any) {
let number = Int(tav.text!)! + 1
eredmeny.text = String(describing: number)
}
}
I ran your code and it works fine if an integer in entered in the textfield. But if you enter any character other than a string then the app crashes because Int(tav.text!)! returns nil and adding 1 to nil. Check for nil first and then add something to it.

UIActivityIndicator shows for a split second

I am trying to show an spinning activity indicator for my app when the user taps on a button before they are presented with the next view - it kind of works, I see it for a split second
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewWillAppear(animated: Bool) {
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Refactor your code. You don't need to stop the activity indicator in viewWillAppear. You can hide it in viewDidLoad. Here's the code:
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
myActivityIndicator.hidden = true
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Make sure that the IBOutlet for the activity indicator and IBAction for getting events are correctly connected in Interface Builder.
I changed it and it doesn't work, until I go back in the navigation
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
viewDidAppear is probably a more appropriate place to stop the animation and hide the spinner.

Variable retaining old value when trying to reset it

I'm pretty new to Swift and programming, so this may seem pretty basic, but I haven't been able to locate an answer on StackOverflow or Google while trying to solve this issue today.
I'm create a basic life counter app for fun, but I'm having some trouble when I'm trying to reset the user's life totals. I'm able to add and subtract totals from the starting value, but when I run reset it the initial value is staying in memory.
For example, if player one has 9 life and player 2 has 0, if I hit the reset button both totals will show 10 (the default), however, if I subtract 1 from player 1 it will show 8 and if I added 1 to player 2 it would show 1 instead of 11.
I guess I need to pass the value from reset somehow or just put the default variable somewhere else? Any help is appreciated. Thanks.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var playerOneAdd: UIButton!
#IBOutlet weak var playerOneMinus: UIButton!
#IBOutlet weak var playerOneLife: UILabel!
#IBOutlet weak var playerTwoMinus: UIButton!
#IBOutlet weak var playerTwoPlus: UIButton!
#IBOutlet weak var playerTwoLife: UILabel!
#IBOutlet weak var resetButton: UIButton!
var playerOne = 10
var playerTwo = 10
override func viewDidLoad() {
super.viewDidLoad()
playerOneLife.transform = CGAffineTransformMakeRotation(3.14)
}
#IBAction func PlayerOnePlusButton(sender: AnyObject) {
++playerOne
playerOneLife.text = String(playerOne)
}
#IBAction func playerOneMinusButton(sender: AnyObject) {
--playerOne
playerOneLife.text = String(playerOne)
}
#IBAction func playerTwoMinusButton(sender: AnyObject) {
--playerTwo
playerTwoLife.text = String(playerTwo)
}
#IBAction func playerTwoPlusButton(sender: AnyObject) {
++playerTwo
playerTwoLife.text = String(playerTwo)
}
#IBAction func resetPressed(sender: AnyObject) {
var playerOne = 10
var playerTwo = 10
playerOneLife.text = String(playerOne)
playerTwoLife.text = String(playerTwo)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When you call var again, you re-define the variable. You should just set it equal.
playerOne = 10
playerTwo = 10
You need to remove the var keyword in resetPressed:
#IBAction func resetPressed(sender: AnyObject) {
playerOne = 10
playerTwo = 10
playerOneLife.text = String(playerOne)
playerTwoLife.text = String(playerTwo)
}
The var keyword defines a new variable. Since the variables are declared inside of a function, they are local variables and they override the instance variables. Local variables are local to a function, so the overridden variables aren't accessed in the other functions.

Resources