Adding points at score (Swift) - swift

I've got a problem with Swift. I want to make a simple (for testing) app that works so: every time you click on the button it the score will be from 0 to 1 to 2 etc.. (every time you click it will add up +1)
#IBAction func buttonPressed(sender: AnyObject) {
ourScore.text = "1"
}
#IBOutlet var ourScore: UILabel!
ourScore.text is currently 0 when you click on the button it will be 1 but how can I add every time on the button so it will adding +1?
Thanks!

Simple solution with a variable
#IBOutlet var ourScore: UILabel!
var score = 0
#IBAction func buttonPressed(sender: AnyObject) {
ourScore.text = "\(++score)"
}

You could do this even though it's not particularly safe (forced unwrap could fail) :
Swift 1 :
#IBAction func buttonPressed(sender: AnyObject) {
ourScore.text = String(ourScore.text.toInt()! + 1)
}
#IBOutlet var ourScore: UILabel!
Swift 2 :
#IBAction func buttonPressed(sender: AnyObject) {
ourScore.text = String(Int(ourScore.text)! + 1)
}
#IBOutlet var ourScore: UILabel!
I'd highly suggest you store the score in an Int variable. That would be a cleaner solution.

Related

printing percentage value into decimal swift

The Result I wantedi have three buttons under same IBAction showing different percentages %0, %10, %20.
and I have another button called calculate in another IBAction. so what I want is. when I choose %10 and press calculate I want to print out the 0.1 in the console. this is very beginner question but I m really stuck here
below is my code
import UIKit
class CalculatorViewController: UIViewController {
#IBOutlet weak var billTextField: UITextField!
#IBOutlet weak var zeroPctButton: UIButton!
#IBOutlet weak var tenPctButton: UIButton!
#IBOutlet weak var twentyPctButton: UIButton!
#IBOutlet weak var splitNumberLabel: UILabel!
#IBAction func tipChanged(_ sender: UIButton) {
zeroPctButton.isSelected = false
tenPctButton.isSelected = false
twentyPctButton.isSelected = false
sender.isSelected = true
}
#IBAction func stepperValueChanged(_ sender: Any) {
}
#IBAction func calculatePressed(_ sender: UIButton) {
}
}
The easiest way is to add the output on the percentage button tapped, e.g. in the #IBAction of the "10%" button:
print("0.1")
If you really need to output on the Calculate button tap, you can first determine which percantage button is selected:
if zeroPctButton.isSelected {
print("0.0")
} else if tenPctButton.isSelected {
// ...
If you don't want to hardcode the output, you need some data to base on. Since you don't show us any model code, we can only rely on the text on the buttons. So, the solution might look something like this (inside the Calculate button's #IBAction):
[zeroPctButton,
tenPctButton,
twentyPctButton]
.filter { $0.isSelected }
.forEach {
print("\(Double(($0.titleLabel.text ?? "").filter { ("0"..."9").contains($0) }) / 100.0)")
}
(It takes the text of the selected percentage buttons, filters out only digits from it, converts the resulting digit string to Double, and divides the result by 100.0.)

Troubles with starting value using UISlider

Started working on the application. There was the following problem, in Main.storyboard the current value is set for the slider (for the top one it is 1.5 out of 3, and for the bottom one it is 100 out of 200), therefore, in the screenshot, I should have points on the slider track in the middle. I started googling about it, I can't find anything. Xcode 12 problem maybe? If not, please help me write the correct search query. I will be very grateful.
Sorry for my bad English. :)
Here ViewController:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var heightLabel: UILabel!
#IBOutlet weak var weightLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func heightSliderChanged(_ sender: UISlider) {
print(sender.value)
heightLabel.text = String(format: "%.2f", sender.value) + "m"
}
#IBAction func weightSliderChanged(_ sender: UISlider) {
weightLabel.text = String(format: "%.0f", sender.value) + "Kg"
}
}
Property's for first slider:
Property's for second slider:
Yes, it's Xcode issues. You can find the issues list from this link. (https://fahimfarook.medium.com/xcode-12-and-ios-14-developer-bugs-and-issues-ada35920a104).
Create an outlet of both slider and set value through coding.
#IBOutlet weak var firstSlider: UISlider!
#IBOutlet weak var secondSlider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
firstSlider.minimumValue = 0
firstSlider.maximumValue = 3
firstSlider.value = 1.5
secondSlider.minimumValue = 0
secondSlider.maximumValue = 200
secondSlider.value = 100
}

Check if timer is finished

I'm sandboxing a little timer app.
I'm using this cocoapod
This is my code so far:
import UIKit
import CountdownLabel
class ViewController: UIViewController {
#IBOutlet weak var moreBtn: UIButton!
#IBOutlet weak var lessBtn: UIButton!
#IBOutlet weak var gifview: UIImageView!
#IBOutlet weak var countdownLabel: CountdownLabel!
var mins = 25
override func viewDidLoad() {
super.viewDidLoad()
setupCountdown()
updateTimer(min: mins)
}
// MARK: Buttons
#IBAction func startPressed(_ sender: Any) {
countdownLabel.start()
}
#IBAction func lessPressed(_ sender: Any) {
if(mins > 0){
mins = mins - 5
updateTimer(min: mins)
}
}
#IBAction func morePressed(_ sender: Any) {
mins = mins + 5
updateTimer(min: mins)
}
//MARK: Helper Func
func updateTimer(min: Int){
countdownLabel.setCountDownTime(minutes: 60*Double(min))
}
func setupCountdown(){
countdownLabel.textColor = .black
countdownLabel.font = UIFont(name:"Courier", size:UIFont.labelFontSize)
countdownLabel.animationType = .Evaporate
}
}
Now I want to check if the timer is finished (can use this cocoapod built in function: countdownLabel.isFinished() ).
But I have no clue WHERE and HOW I can check this, e.g. in viewDidLoad() I can’t check .isFinished()....
As an example: if(countdownLabel.isFinished()){countdownLabel.text = "Finished"}
WHERE to insert this line in the code??
I would be very happy if you can quickly copy and paste the code as an answer - with the mentioned line correctly inserted :)
Thank you!
You can use delegate method countdownFinished() of CountdownLabel to check when counter finished as mentioned in the example of this pod. Your updated code as below
import UIKit
import CountdownLabel
class ViewController: UIViewController {
#IBOutlet weak var moreBtn: UIButton!
#IBOutlet weak var lessBtn: UIButton!
#IBOutlet weak var gifview: UIImageView!
#IBOutlet weak var countdownLabel: CountdownLabel!
var mins = 25
override func viewDidLoad() {
super.viewDidLoad()
setupCountdown()
updateTimer(min: mins)
}
// MARK: Buttons
#IBAction func startPressed(_ sender: Any) {
countdownLabel.start()
}
#IBAction func lessPressed(_ sender: Any) {
if(mins > 0){
mins = mins - 5
updateTimer(min: mins)
}
}
#IBAction func morePressed(_ sender: Any) {
mins = mins + 5
updateTimer(min: mins)
}
//MARK: Helper Func
func updateTimer(min: Int){
countdownLabel.setCountDownTime(minutes: 60*Double(min))
}
func setupCountdown(){
countdownLabel.textColor = .black
countdownLabel.font = UIFont(name:"Courier", size:UIFont.labelFontSize)
countdownLabel.animationType = .Evaporate
countdownLabel.countdownDelegate = self //>>>>>> added this line to your code
}
}
//>>>>>>added this function to your code
extension ViewController: CountdownLabelDelegate {
func countdownFinished() {
debugPrint("countdownFinished at delegate.")
}
}
I think what you are looking for is a 'listener' for when it reaches 0 ("Finished")
From the docs (they only show on various times, though for 0 you should use
countdownLabel.then(0) { [unowned self] in
countdownLabel.text = "Finished"
}

How to get all iterations of a loop to a text field in swift xcode

First ever solo app attempt, creating a very simple basic app. Noob alert!
I am trying to get all iterations of this for in loop to the text field. Only the last value shows up whereas when printed they all show up. Any noob guidance is appreciated :)
#IBOutlet weak var shelfSize: UITextField!
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
shelfSize.delegate = self
super.viewDidLoad()
}
#IBAction func enterTapped(_ sender: UIButton) {
calculateCoursing()
}
func calculateSizing() {
let shelfHeight = Int(shelfSize.text!)
let veryTight = shelfSize! + 8
for index in 1...10 {
print("Course \(index) = \(index * veryTight)")
textView.text = "Course \(index) = \(index * veryTight)"
}
}

Swift how to multiply value of quantity put in textfield with UIStepper

I working on a Swift app that uses a UIStepper , for user to enter a quantity into textField, how can I take the quantity and multiply by value stored in var?
Here is what I have so far
var value1 = 4 // value to multiply //
#IBOutlet weak var oneValue : UITextField!
#IBOutlet weak var oneClicker: UIStepper!
#IBOutlet weak var totalTextField: UITextField! // show total value1 * oneValue //
#IBAction func oneClicker(sender: UIStepper) {
// puts quantity into oneValue textField //
self.oneValue.text = Int(sender.value).description
}
#IBAction func calculateButton(sender: UIButton) {
// just can't figure out method? //
}
//Thanks for help//
#IBAction func calculateButton(sender: UIButton) {
println("\(calculate(self.oneValue.text.toInt()!)")
}
func calculate(value: Int) -> Float {
return Float(value * value1)
}
There you go.