Reset Score Button iOS - ios

I want displayBatsmenOneScoreLabel to reset to 0 when the resetScoreButton is hit.
What would be my code for the resetScoreButton?
Here's my code.
var batsmenOneScoreInt: Int = 0
#IBOutlet weak var displayBatsmenOneScoreLabel: UILabel!
#IBAction func BatsmenOneScoreStepper(_ sender: UIStepper) {
let batsmenOneScoreValue = Int(sender.value)
displayBatsmenOneScoreLabel.text = String(batsmenOneScoreValue)
}
#IBAction func resetScoreButton(_ sender: Any) {
//what should I write here...
}

You need to reset the batsman score, it should work.
You can try this code:
#IBAction func resetScoreButton(_ sender: Any) {
let batsmenOneScoreValue = 0
displayBatsmenOneScoreLabel.text = String(batsmenOneScoreValue)
}

For good practice you should first reset the main variable that is holding the score, and then set that variable to the label.
#IBAction func resetScoreButton(_ sender: Any) {
batsmenOneScoreInt = 0;
displayBatsmenOneScoreLabel.text = "\(batsmenOneScoreInt)"
}

#IBOutlet weak var batsmenScoreStepper:UIStepper!
#IBAction func resetScoreButton(_ sender: Any) {
batsmenScoreStepper.value = 0.0;
displayBatsmenOneScoreLabel.text = "\(batsmenScoreStepper.value)"
}
you should first take outlet of your UIStepper and reset it.

You can reset score in button's action(touchup inside) like below...
#IBAction func resetScoreButton(_ sender: Any) {
displayBatsmenOneScoreLabel.text = "0"
//do something here...
}
EDIT:
In case if you want to reset score variable you need to set batsmenOneScoreInt = 0 in resetScoreButton method.

Related

How to go through array with button press

I have an array that contains different information,
how can I iterate through the array with a button press? I have two buttons, and I need to allow one to move forward in the array and the back button to display the previous index.
#IBOutlet weak var backButton: UIButton!
#IBOutlet weak var nextButton: UIButton!
#IBOutlet weak var infoLabel: UILabel!
#IBOutlet weak var pageControl: UIPageControl!
Let infoArray = ["info1","info2","info3","info4"]
#IBAction func nextTapped(_ sender: Any) {
// Change the label based on the selected index in array
}
#IBAction func backTapped(_ sender: Any) {
//Return to previous index and update label text
}
I also added page control for better UX, but for now I'm just worried about learning how to even change the label through button Tap.
My guess would be to start the index at 0 which would be info1 and go from there. I can worry about saving index state later.
Any help is much appreciated.
The logic should look something like this
let infoArray = ["info1","info2","info3","info4"]
func viewDidLoad() {
pageControl.numberOfPages = infoArray.count
pageControl.currentPage = 0
}
#IBAction func nextTapped(_ sender: Any) {
// Change the label based on the selected index in array
guard pageControl.currentPage + 1 < infoArray.count else {
return
}
pageControl.currentPage += 1
}
#IBAction func backTapped(_ sender: Any) {
//Return to previous index and update label text
guard pageControl.currentPage - 1 >= 0 else {
return
}
pageControl.currentPage -= 1
}
When a user taps a page control to move to the next or previous page, the control sends the valueChanged event for handling by the delegate. The delegate can then evaluate the currentPage property to determine the page to display. The page control advances only one page in either direction.
Please check the below code you can do something like this to move your array forward and backward. Please add other checks according to your need.
var i = 0
let infoArray = ["info1","info2","info3","info4"]
#IBAction func nextTapped(_ sender: Any) {
// Change the label based on the selected index in array
if i >= 0 && i < infoArray.count{
dataLbl.text = infoArray[i]
}
if i > 3 {
i = i-1
} else {
i = i+1
}
}
#IBAction func backTapped(_ sender: Any) {
//Return to previous index and update label text
if i >= 0 && i < infoArray.count-1 {
dataLbl.text = infoArray[i]
}
if i < 0 {
i = i+1
} else {
i = i-1
}
}

Is hidden not working on image views in Swift 3?

#IBOutlet weak var allStocksSelected: UIImageView!
#IBOutlet weak var shortStocksSelected: UIImageView!
#IBOutlet weak var longStocksSelected: UIImageView!
#IBOutlet weak var myStocksTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
myStocksTableView.delegate = self
myStocksTableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
self.shortStocksSelected.isHidden = true
self.longStocksSelected.isHidden = true
self.allStocksSelected.isHidden = true
}
#IBAction func allStocksButtonPressed(_ sender: Any) {
print("ALL!")
self.stockTypeChanged(stockType: allStocksSelected)
}
#IBAction func shortStocksButtonPressed(_ sender: Any) {
print("SHORT!")
self.stockTypeChanged(stockType: shortStocksSelected)
}
#IBAction func longStocksButtonPressed(_ sender: Any) {
print("LONG!")
self.longStocksSelected.isHidden = false
self.shortStocksSelected.isHidden = true
self.allStocksSelected.isHidden = true
//stockTypeChanged(stockType: longStocksSelected)
}
#IBAction func addNewStockButtonPressed(_ sender: Any) {
}
#IBAction func signOutButtonPressed(_ sender: Any) {
}
private func stockTypeChanged(stockType: UIImageView) {
let stockTypes: [UIImageView] = [allStocksSelected, shortStocksSelected, longStocksSelected]
for (stock) in stockTypes {
if (stock == stockType) {
stock.isHidden = false
} else {
stock.isHidden = true
}
}
}
As shown from my code above, I am basically just trying to hide and show certain image views when buttons are pressed.
I am 100% sure that all of the buttons actions and IB outlets are properly connected, yet the image views are still not hiding and showing, and I cannot figure out why.
I was running into this same issue. Placing your isHidden code to execute on the main thread should solve the problem.
DispatchQueue.main.sync {
}

Code crashing in during runtime

I am learning swift3 programming but after executing my calculator app its crashing in between. Please check the below code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var aLabel: UILabel!
#IBOutlet weak var commmon_button: UIButton!
var a: Int?
var b: Int?
var sum: Int?
var val = ""
#IBOutlet weak var text_feild: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func clik_button(_ sender: UIButton) {
val=String(sender.tag)
text_feild.text = text_feild.text! + val
}
#IBAction func fn_addition(_ sender: UIButton) {
a = Int(text_feild.text!)
}
#IBAction func fn_answer(_ sender: UIButton) {
b = Int(text_feild.text!)
sum = a! + b!
a = 0
b = 0
text_feild.text = nil
text_feild.text = String(sum!)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
During run time i am getting crash at fn_addition by saying Thread 1 breakpoint 2.1
Initiliaze your variables as below
var a = 0
var b = 0
var sum = 0
Replace your methods with below methods.
#IBAction func clik_button(_ sender: UIButton) {
guard let value = String(sender.tag), let text = text_feild.text
else {
return
}
text_feild.text = text + value
}
#IBAction func fn_addition(_ sender: UIButton) {
guard let aValue = Int(text_feild.text)
else{
return
}
a = aValue
}
#IBAction func fn_answer(_ sender: UIButton) {
guard let bValue = Int(text_feild.text)
else{return}
b = bValue
sum = a + b
a = 0
b = 0
text_feild.text = ""
text_feild.text = String(sum)
}
Suggestion: You are using same text field for taking a , b values and for showing sum result. It is better to use two different text fields for taking a and b values separately. Take a label to show sum value.

How to use one button to add scores for different integers (swift3)

Right now i have a purple and red score. I would like to use the 1 and 2 buttons to add the score for the colors. I know how to do this by creating just a sets of buttons to the specific color but doing this again and again will create problems. So what I am trying to do is if purple is selected and if and only if 1 is pressed then the total score of purple will = 1. I guess what I am trying is perform a switch statement for the buttons.
Before purple/red button is pressed.
After purple/red button is pressed.
import UIKit
class ViewController: UIViewController {
var purpleScore = 0
var redScore = 0
#IBOutlet var plus1: UIButton!
#IBOutlet var plus2: UIButton!
#IBOutlet var addRed: UIButton!
#IBOutlet var addPurlple: UIButton!
#IBOutlet var totalScorePurple: UILabel!
#IBOutlet var totalScoreRed: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
plus1.isHidden = true
plus2.isHidden = true
}
#IBAction func enteringRed(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func enteringPurple(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func plus1(_ sender: Any) {
}
#IBAction func plus2(_ sender: Any) {
}
}
If I understand correctly as the comment above, this should work
Create 2 Bool for red and purple to see weather they are checked or not, this is easiest way, else you can just check the redButton color got changed (if it does) or .selected = true
var redChecked = false
var purpleChecked = false
#IBAction func enteringRed(_ sender: Any) {
redChecked = !redChecked
//change color etc..
}
#IBAction func enteringPurple(_ sender: Any) {
purpleChecked = !purpleChecked
//change color etc..
}
#IBAction func plus1(_ sender: Any) {
if redChecked {
redScore+=1
}
if purpleChecked {
purpleScore+=1
}
}
#IBAction func plus2(_ sender: Any) {
if redChecked {
redScore+=2
}
if purpleChecked {
purpleScore+=2
}
}

Swift - Printing out the current value of a number

I'm currently developing a counter app which every time you press a button it will count up. I have already done this and works 100%, what I'm trying to do is make a another button and when you press it, it shows the current number on the console but it only prints out 0 because that's the default variable I assigned the value to.
My whole class:
var counterNumber = 0
#IBOutlet weak var counterLabel: UILabel!
func initCount(){
counterNumber = 0
}
func numberUp(){
self.counterNumber++;
counterLabel.text = "\(self.counterNumber)"
}
#IBAction func CountUp(sender: UIButton) {
numberUp()
}
#IBAction func RestartButton(sender: UIButton) {
initCount()
}
#IBAction func printButton(sender: UIButton) {
self.numberUp();
print(self.counterNumber)
}
override func viewDidLoad() {
super.viewDidLoad()
initCount()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Just call method numberUp in printButton: action.
var counterNumber = 0//this is a variable of class
#IBOutlet weak var counterLabel: UILabel!
#IBAction func printButton(sender: UIButton) {
self.numberUp();
print(self.counterNumber)
}
func numberUp(){
self.counterNumber++;
counterLabel.text = "\(self.counterNumber)"
}
Use 'self' keyword to call instance variables.
class ViewController: UIViewController {
// Properties
var counterNumber:Int = 0 // Make this variable Global to Class
// IBOutlets Properties
#IBOutlet weak var counterLabel: UILabel!
#IBAction func printButton(sender: UIButton) {
self.numberUp()
self.counterLabel.text = "\(self.counterNumber)"
print("\n You Pressed ==> \(sender.title) button \(self.counterNumber) times")
}
func numberUp() {
self.counterNumber += 1
self.counterLabel.text = "\(counterNumber)"
}
}

Resources