X.BOZO X.BOZO - 7 months ago 8
Swift Question

How can I make a button to retrieve the latest information set?

How can I make a button to retrieve the latest information set
For example, when I write Number: 1
After this write: 2

And: 3

And: 4

I want a button retrieves the last number I write before Number: 4

image

Here is my code

import UIKit

class ViewController: UIViewController {

var actionString : String?
@IBOutlet weak var textfiled: UITextField!
@IBOutlet var lblzerous: UILabel!
@IBOutlet var lbl: UILabel!
@IBOutlet var button: UIButton!


override func viewDidLoad() {
super.viewDidLoad()


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func me() {
lbl.text! = lbl.text! + "\n" + textfiled.text! + "\n" + "----"
lbl.numberOfLines = 0
self.lblzerous.text = String(CInt(self.textfiled.text!)! + CInt(self.lblzerous.text!)!)

}
@IBAction func button(sender: AnyObject) {


if textfiled.text! == "" {


let alertController = UIAlertController(title: "Error", message: " write the number ", preferredStyle: UIAlertControllerStyle.Alert)

let cancelAction = UIAlertAction(title: "ok", style: .Cancel) { (action) -> Void in
self.actionString = "Cancel"


}
alertController.addAction(cancelAction)

self.presentViewController(alertController, animated: true, completion: nil)
return
}
me()

if Int(lblzerous.text!) >= Int("152") {


let alertController = UIAlertController(title: "Gmae Over", message: " ... \(lblzerous.text!)", preferredStyle: UIAlertControllerStyle.Alert)

let cancelAction = UIAlertAction(title: "...", style: .Cancel) { (action) -> Void in
self.actionString = "Cancel"

}
alertController.addAction(cancelAction)

self.presentViewController(alertController, animated: true, completion: nil)

} else {
print("keep going ....!")
}
}

@IBAction func button1(sender: AnyObject) {
// Do SomeCode

}
}

Answer

Add an array variable to serve you as a stack from which you revert the changes:

var previousValues: [String] = [String]();
var actionString : String?
@IBOutlet weak var textfiled: UITextField!
@IBOutlet var lblzerous: UILabel!
@IBOutlet var lbl: UILabel!
@IBOutlet var button: UIButton!

Then in me() do:

func me() {
    lbl.text! =  lbl.text! + "\n" + textfiled.text! + "\n" + "----"
    lbl.numberOfLines = 0
    self.lblzerous.text =  String(CInt(self.textfiled.text!)! + CInt(self.lblzerous.text!)!)
    previousValues.append(textField.text ?? "error");

}

Edit: Finally in your retreat button do:

@IBAction func button1(sender: AnyObject) {
    if previousValues.count > 0 {
        let previousValue = previousValues.removeLast();
        lbl.text! = lbl.text! + "\n-" + previousValue + "\n" + "----";
        let subtracted = (Int(lblzerous.text!)!) - (Int(previousValue)!);
        self.lblzerous.text = String(subtracted)
    }
}

Edit2: Give default values to your labels:

override func viewDidLoad() {
        super.viewDidLoad()
        lblzerous.text = "0";
        lbl.text = "0";
    }