I made an app where I save a label that I can make it go up or down one number in my app. But if I lose out of my app and open it back up, if i make the label go up again than it resets back to zero and goes to one. This is because I have to set the variable value to zero. Here is my code:
#IBOutlet var goal: UILabel!
#IBAction func player1button(sender: AnyObject)
{
NSUserDefaults.standardUserDefaults().setValue(goal.text!, forKey:"firstGoal")
}
var goal1 = 0
#IBAction func goalUp(sender: AnyObject)
{
goal1++
goal.text = "\(goal1)"
}
override func viewDidLoad()
{
super.viewDidLoad()
goal.text = (NSUserDefaults.standardUserDefaults().objectForKey("firstGoal") as? String)
}
Im saving the goal number then calling it back later in the text. Please show me a way to fix it so it just adds on to the previous number.
in viewDidLoad() add
goal1 = Int((NSUserDefaults.standardUserDefaults().objectForKey("firstGoal") as? String)!)
this will set the int variable you are using to keep track of the goals to the correct value when the app loads. currently you are only setting the text label to the correct value.
or you can simply make var goal1 a global variable , just after the imports and before the class keyword , in case you don't need to keep the data till the next time you open the app itself
Related
I want to listen the content changes in the UIlabel and prevent it from changing when its length is greater than 7. What should I do? I tried set / get / willset / didset. It seems that it can't meet my needs
I wrote a simple demo ,When I press the button, add a number to display area. I don't want his length to exceed my expectations
In my real project, I'm developing a calculator
What I can think of is to judge the length of displayValue, but doing so will make my code wordy
didSet can help you better.
add this code in line number 6 in your code.
var displayValue: String = "" {
didSet {
if displayValue.count <= 7 {
customLabel.text = displayValue
}
}
}
Then in your action function, you need to just do it.
#IBAction func clickBtn( _ sender: Any) {
displayValue = displayValue + "0"
}
For counting the characters inside a label you use the count method in the text property like this
#IBOutlet weak var testLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
testLabel.text = "duck"
print(testLabel.text.count) //return 4
}
For user action you can't use the UILabel since it's not user interactive, you have to use the UITextField and connect to him an UIAction or explain us better what you want to do.
I want the user to choose the preferred color by clicking the wanted button, and based on the choice it will be the background color of all pages in the App.
The code given is what I have used to change the background color through code, as shown in the image attached Screenshot of page sample . However, it changes temporary only and does not get saved when I move to another page and get back.
class ColorViewController: UIViewController {
#IBOutlet weak var redBtn: UIButton!
#IBOutlet weak var blueBtn: UIButton!
#IBAction func changeToRed(_ sender: UIButton) {
self.view.backgroundColor = UIColor.green
}
#IBAction func changeToBlue(_ sender: UIButton) {
self.view.backgroundColor = UIColor.cyan
}
If you also know how to save this color for a registered user (So every time the user signs in, the chosen background stays as is instead of reseting to default) it would be great. Please advise if you know the solution, I am new on Swift.
For saving user's color choice, you can use UserDefaults, that will allow you to store small sized information (like color preference) inside memory. So you can use something like this:
UserDefaults.standard.set(UIColor.red , forKey: "themePreferenceKey")
That will store Red choice in memory that'll be reachable with key themePreferenceKey later on.
For changing view/button background colors according to user's saved choice, you must fetch the value of themePreferenceKey whenever a view is Appeared, and change the colors accordingly. This can be implemented inside viewDidAppear method. Such as:
override func viewDidAppear(_ animated: Bool) {
if let preferredColor = UserDefaults.standard.object(forKey: "themePreferenceKey") as? UIColor {
self.view.backgroundColor = preferredColor
}
}
Im currently developing a iOS app in which im going to keep track of our warehouse stock. It's a pretty simple app an just contains a lable and an stepper. The app is pretty much finished, but i don't get how to save the changed value of the label. I want to save it automatically so that when someone presses the "+" on the stepper, the value should save without pressing a extra save button
Current code:
//montageplatte
#IBOutlet weak var lbl_montageplatte: UILabel!
#IBAction func stepper_montageplatte(_ sender: UIStepper)
{
lbl_montageplatte.text = Int(sender.value).description
}
you can save it in UserDefaults.
#IBAction func stepper_montageplatte(_ sender: UIStepper) {
lbl_montageplatte.text = Int(sender.value).description
UserDefaults.standard.set(String(sender.value), forKey: "lblMontageplatte")
}
To get back value you can do as follow...
if let lblValue = UserDefaults.standard.object(forKey: "lblMontageplatte") as? String {
print(lblValue)
lbl_montageplatte.text = lblValue
}
Simple solution:
In the custom cell create an outlet for the stepper and a NSKeyValueObservation property
#IBOutlet weak var stepper : UIStepper!
var stepperObservation : NSKeyValueObservation?
In cellForRow in the controller add the key value observer
cell.stepperObservation = cell.stepper.observe(\.value, options: [.new]) { (stepper, change) in
print(change.newValue!)
}
Rather than printing the new value update the model (the item for the particular index path) and save the datasource array if necessary.
I am Currently working on a swift project in XCode, and I've encountered an issue where I cannot increment an array's one element by through various endeavours.
My ViewController.Swift
import UIKit
class FailureViewController: UIViewController {
#IBOutlet weak var failFactLabel: UILabel!
let failFact = Failure()
override func viewDidLoad() {
super.viewDidLoad()
failFactLabel.text = failFact.failArray[0]
}
#IBAction func showFunFact() {
//this is where the issue is
}
}
For the function showFunFact I have previously tried
for var x = 0; x < failArray.cout; x++ {
failFactLabel.text = failFact.failArray[x]
}
but encountered the error: "Use of Unresolved Identifier".
Putting this aside, I decided to use
for var x = 0; x < 10; {
x+=1
}
Although this does not generate an error however, I see it stops at the first element in my array. I tried +=5 and it displays the 5th element in my array once, I believe this code runs through once. I need it to be a consistently working piece so it continuously displays the next element, I am stumped because this should theoretically work since it is called a "Loop" Any help or suggestion is Appreciated!
Objective Redefined:
I am trying to show one fact at a time, before this I have used a random function to generate a random fact every time however things tend to get repetitive and therefore I decided to start on the initial array index is 0 and grab the next fact (next element in array) every time the button is pressed (Thanks to Luk's Question)
You need to create an instance variable to hold the current factIndex, if that is what you actually are trying:
import UIKit
class FailureViewController: UIViewController {
#IBOutlet weak var failFactLabel: UILabel!
var factIndex = 0
let failFact = Failure()
override func viewDidLoad() {
super.viewDidLoad()
failFactLabel.text = failFact.failArray[factIndex]
}
#IBAction func showFunFact() {
factIndex++
if factIndex >= failFact.failArray.count then {
factIndex = 0
}
failFactLabel.text = failFact.failArray[factIndex]
}
}
I have a button that is supposed to append the value of the cell's label to an array.
#IBAction func test(sender: AnyObject) {
var cellLabelArray = [""]
cellLabelArray.append(cell.cellLabel.text)
}
I assumed that as the more I selected the button, the longer the array would become.
Like : cellLabelArray = ["label", "label", "label", "label"] if I pressed it four times.
What I'm getting is that it just appends the text label once, what I want is to append multiple times, because the label changes.
it returns something like this: ["label"] no matter how many times I press the button
How would I do this using Swift?
The problem is that with this line:
var cellLabelArray = [""]
you're emptying cellLabelArray at the start of the method.
So declare and initialize cellLabelArray elsewhere in your code, ex:
var cellLabelArray:[String] = []
#IBAction func test(sender: AnyObject) {
cellLabelArray.append(cell.cellLabel.text)
}