Troubles with starting value using UISlider - swift

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
}

Related

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)"
}
}

Error : Consecutive statements on a line must be separated by ';' What's wrong?

import UIKit
class ViewController: UIViewController {
//Place your instance variables here
let questions = Questionbank()
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet var progressBar: UIView!
#IBOutlet weak var progressLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let firstQuestion = questions.list[0]
questionLabel.text = firstQuestion.questionText
}
#IBAction func answerPressed(_ sender: AnyObject) {
}
func updateUI() {
}
func nextQuestion() {
}
func checkAnswer() {
}
func startOver() {
}
}
This is my code above. And following is the error come up.
This is the errors come up.
There seems to be problem with questionLabel.text line but
I can't figure it out. This is a part of ios tutorial I am currently
working on.
Thank you for help.
I just found out why.
Problem was questionLabel.text not being written using xcode auto complete feature.
First time I typed manually and got error.
Second time, I let Xcode autocomplete it and it worked.
Anyone know why this works this way?

Xcode 7 Swift 2 error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, SUBCODE=0X0)

Ello! I am working on a notes app for mac, using swift as the language. I am currently working on a save and load feature, and have run across an error I am not sure how to fix. Here is the problematic code:
import Cocoa
class NoteViewController: ViewController {
#IBOutlet weak var notefield: NSTextField!
#IBOutlet weak var savenamefield: NSTextField!
#IBOutlet weak var loadnamefield: NSTextField!
#IBAction func save(sender: AnyObject) {
// The line below gets the error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0
NSUserDefaults.standardUserDefaults().setObject(notefield.stringValue, forKey: savenamefield.stringValue)
}
#IBAction func load(sender: AnyObject) {
var name = loadnamefield.stringValue;
var lnote = NSUserDefaults.standardUserDefaults().objectForKey(name)
notefield.stringValue = lnote as! String
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
What might I be doing wrong?
I figured it out. With some testing, I discovered that everything worked until I added notefield.stringValue into the code.I then realized that I had this is a different window. The code is covering 3 different windows, 2 of which are popups. notefield is the only element I had inside the code from the main window. I moved all of the elements to the same window, and recoded the entire thing. I got it working well using the following code:
import Cocoa
class NoteViewController: ViewController {
#IBOutlet weak var notefield: NSTextField!
#IBOutlet weak var savenamefield: NSTextField!
#IBOutlet weak var loadnamefield: NSTextField!
#IBAction func save(sender: AnyObject) {
NSUserDefaults.standardUserDefaults().setObject(notefield.stringValue, forKey: savenamefield.stringValue)
}
#IBAction func load(sender: AnyObject) {
notefield.stringValue = NSUserDefaults.standardUserDefaults().objectForKey((loadnamefield?.stringValue)!) as! String
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
Please note that I am supplying this code as a guideline only. Thanks for the help that was given.

Adding points at score (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.