I want the Points number to appear on a label which I have called the score. It keeps coming up with errors. I have tried multiple different ways but every time it pulls an error. Can someone resolve this, please?
//VARIABLES
var currentQuestion = 0
var rightAnswerPlacement:UInt32 = 0
var points:UInt32 = 0
//SCORE
#IBOutlet weak var score: UILabel!
//QUESTION
#IBOutlet weak var lbl: UILabel!
//BUTTON
#IBAction func action(_ sender: AnyObject)
{
if (sender.tag == Int(rightAnswerPlacement))
{
print("Right")
points += 1
}
else
{
print("Wrong")
}
if (currentQuestion != questions.count)
{
newQuestion()
}
else
{
score.text = points
}
}
You can't set a UILabel .text property (which is a String) to an Int. You need to make it a String first. Like this:
score.text = "\(points)"
The text property of the UILabel is of type String, so you need to cast points to String before setting it to score.text. Replace:
score.text = points
With:
score.text = "\(points)"
Or
score.text = String(points)
As a side note, in Swift you can write your if statements like this:
if currentQuestion != questions.count {
}
Related
I want to hide a UIButton as long as a certain variable has not reached a certain value.
I tried some setHidden and isHidden setups, but I guess I did it all wrong. I don't exactly know where to put these commands.
I have looked for a solution at almost every webpage but I got no idea what everybody is talking about.
import UIKit
class ViewController: UIViewController {
let startvalue = 1000000
#IBOutlet weak var Counter: UITextField!
#IBAction func Button(_ sender: UIButton) {
if number > 1{
number -= 1
Counter.text = String(number)
} else {
Counter.text = "Hurray"
}
}
#IBOutlet weak var Reset: UIButton!
#IBAction func Reset(_ sender: UIButton) {
if number == 1{
number = startvalue
Counter.text = String (number)
}
}
override func viewDidLoad() {
var number = startvalue {
didSet {
if number < 1 {
Reset.isHidden = false
}
}
}
super.viewDidLoad()
}
}
I don't now what I need to put where to hide Result as long as number is greater than 1.
Handle it in the didSet of the variable.
var number = startValue {
didSet {
if number < 1 {
yourButton.isHidden = false
}
}
}
Now, whenever your number is changed, the didSet block will check if the value is less than 1. If so, it'll unhide the button.
Note: Make sure the button is hidden before the view shows. Also, make sure to hide the button if needed on resetting.
Use lowerCamelCase for variable names like they say in the API design guidelines.
I work on a e-commerce project and now designing basket page. I create 4 steppers for determine to quantity of products. I create labels for show cost of each products and one label for total cost.
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
#IBOutlet weak var label4: UILabel!
#IBOutlet weak var label5: UILabel!
#IBAction func GmS1(_ sender: GMStepper) {
label1.text = String(sender.value*1.5)
}
#IBAction func GmS2(_ sender: GMStepper) {
label2.text = String(sender.value*0.89)
}
#IBAction func GmS3(_ sender: GMStepper) {
label3.text = String(sender.value*26)
}
#IBAction func GmS4(_ sender: GMStepper) {
label4.text = String(sender.value*4)
}
I don't want to use button but I struggling in addition total cost automatically. My codes are above. Are there anybody know : How can ı addition 4 label in 1 label automatically(without any button).
Label5 for total cost.
You can check photo for general idea.
Make a function sumValues, add all the label's value in it, and assign string of sum to label5.text. This function can be then called from all the IBAction
#IBAction func GmS4(_ sender: GMStepper) {
label4.text = String(sender.value*4)
sumValues()
}
In the function sumValues(),
set label5.text = string of sum of all the labels'.
SideNote - Make sure to initialize value of all labels to 0 in viewDidLoad().
You can try to call this method from every action
func sumAll() {
let sum = [label1,label2,label3,label4].map { Int($0.text ?? "0" ) ?? 0 }.reduce(0,+)
label5.text = "\(sum)"
}
Note: if word variable is part of the label text you may extract number from the text or declare the steppers as outlets and ask them about their current value and at this way use
let sum = [stepper1,stepper2,stepper3,stepper4].map{$0.value}.reduce(0,+)
Continuing from your previous question:
#IBAction func gmstp1(_ sender: GMStepper) {
label1.text = String(sender.value * 1.5)
calculateSum()
}
#IBAction func gmstp2(_ sender: GMStepper) {
label2.text = String(sender.value * 0.89)
calculateSum()
}
#IBAction func gmstp3(_ sender: GMStepper) {
label3.text = String(sender.value * 26.0)
calculateSum()
}
#IBAction func gmstp4(_ sender: GMStepper) {
label4.text = String(sender.value * 4.0)
calculateSum()
}
Where calculateSum is a function defined like so:
func calculateSum() {
if let text1 = label1.text, let value1 = Double(text1),
let text2 = label2.text, let value2 = Double(text2),
let text3 = label3.text, let value3 = Double(text3),
let text4 = label4.text, let value4 = Double(text4) {
let sum = value1 + value2 + value3 + value4
label6.text = String(sum)
}
}
Or if you'd prefer a one-liner:
func calculateSum() {
label5.text = String([label1, label2, label3, label4].reduce(0,{$0 + (Double($1.text ?? "0") ?? 0)}))
}
I am new to Swift and I am coding on Xcode. I am trying to do like a "Hello, World!" in Swift by having an app that is just one screen, one button, and one display label.
My objective is to have three different messages appear with the click of the button. To clarify, each time I click the button it would cycle through the messages.
I tried solving it with a switch, here is my (unfinished attempt)
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var textBox: UILabel!
var userTapped = false
#IBAction func tappedMe(_ sender: UIButton) {
let me =
if userTapped {
switch me {
case me : textBox.text = "Hello, World!"
case me : textBox.text = "Hello, Human!"
case me : textBox.text = "OK scram."
default : break
}
userTapped = true
}
}
}
I'm not sure what I can make me equal to, or if my solution is even a good one.
class ViewController: UIViewController {
#IBOutlet weak var textBox: UILabel!
let strings = ["Hello, World!", "Hello, Human!", "OK scram."]
var tapCount = 0
#IBAction func tappedMe(_ sender: UIButton) {
textBox.text = strings[tapCount]
tapCount = (tapCount + 1) % strings.count
}
}
You could do something like this it would accomplish your goal if I understand it correctly:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var textBox: UILabel!
// Integer to track number of times button has been tapped
var tapCount = 0
// Max total of text variations, for this case 2 (you have three different values, but always start with zero)
let max = 2
#IBAction func tappedMe(_ sender: UIButton) {
// Try different conditions for value of tapCount
switch tapCount {
case 0:
textBox.text = "Hello, World!"
case 1:
textBox.text = "Hello, Human!"
case 2:
textBox.text = "OK scram."
default : break
}
// If this is the max number of times you want to display changing text, reset tapCount to zero, otherwise increment tapCount.
if tapCount < max {
tapCount += 1
} else {
tapCount = 0
}
}
}
Im trying to make a Segmented Control that holds two values, but I'm having trouble making it. I want to multiply the input by whatever side is picked in the segmented view and displayed in the label, but I keep running into errors. Any help?
#IBOutlet var input: UITextField!
#IBOutlet var output: UILabel!
#IBOutlet var controller: UISegmentedControl!
#IBAction func convert(sender: UIButton) {
}
#IBAction func change(sender: AnyObject) {
if controller.selectedSegmentIndex == 0 {
result = number1 * 1
}
if controller.selectedSegmentIndex == 1 {
result = number1 * 5
}
let number1 = Double(input.text!)
label.text = "\(result)"
}
Why you use "number1" before declaration?
Try this:
#IBOutlet var input: UITextField!
#IBOutlet var output: UILabel!
var result:Double! //fixed "cannot be applied to Double and Int." problem
#IBOutlet var controller: UISegmentedControl!
#IBAction func convert(sender: UIButton) {
}
#IBAction func change(sender: AnyObject) {
let number1 = Double(input.text!)
if controller.selectedSegmentIndex == 0 {
self.result = number1 * 1
}
if controller.selectedSegmentIndex == 1 {
self.result = number1 * 5
}
label.text = "\(result)"
}
So I'm trying to create a simple function in my app in which a button adds 1 integer to the label when it's pressed and the other one clears it.
Here is the code:
class ViewController: UIViewController {
var number = 0
#IBOutlet weak var tapCount: UILabel!
#IBAction func plusTapped(sender: AnyObject) {
number = number + 1
tapCount.text = String(number)
}
#IBAction func minusTapped(sender: AnyObject) {
var totalNumber = number - number
tapCount.text = String(totalNumber)
}
However the label does clear when the minus button is tapped but when the plus button is tapped after, it adds up from the previous number before the minus button was pressed.
How do I fix this in a way so that it actually clears the label?
#IBAction func minusTapped(sender: AnyObject) {
number = 0 // add this line to reset the counter, the real number
tapCount.text = String(number)
}
Try
#IBAction func minusTapped(sender: AnyObject) {
number = 0
tapCount.text = String(0)
}
I think it's because when you're handling tap on minus you are altering local variable and not changing number itself. You should change "number" too. Se my comments in your's code
class ViewController: UIViewController {
var number = 0
#IBOutlet weak var tapCount: UILabel!
#IBAction func plusTapped(sender: AnyObject) {
number = number + 1
tapCount.text = String(number)
}
#IBAction func minusTapped(sender: AnyObject) {
//here totalNumber is changed but number is still the same
var totalNumber = number - number
tapCount.text = String(totalNumber)
}