如何创建一个按钮来检索最新的信息集?

时间:2016-04-22 12:06:28

标签: ios swift

如何创建一个按钮来检索最新的信息集 例如,当我写Number:1 写完之后:2

并且:3

并且:4

我想要一个按钮检索我在Number:4

之前写的最后一个数字

image

这是我的代码

导入UIKit

类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

}
 }

1 个答案:

答案 0 :(得分:1)

添加一个数组变量,作为一个堆栈,您可以从中还原更改:

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

然后在 me()中执行:

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

}

修改:最后在撤退按钮中执行:

@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)!);
            lblzerous.text = "\(subtracted)"
        }
    }

Edit2 :为标签指定默认值:

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