ios - 如何制作一个按钮来检索最新的信息集?

标签 ios swift

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

和:3

和:4

我想要一个按钮来检索我在 Number: 4 之前写的最后一个数字

image

这是我的代码

导入 UIKit

类 View Controller :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

}
 }

最佳答案

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

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

关于ios - 如何制作一个按钮来检索最新的信息集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36793428/

相关文章:

ios - 在 AppSync 中获取 "Software caused connection abort"

iOS 使用 Quartz 2D 绘制形状 - 无效的上下文错误

swift - 在 Xcode 中检查 URL 文本作为字符串

ios - 如何在 Swift 3 中加载自定义包

ios - AKCallbackInstrument midiIn 和回调状态不起作用

iphone - UICollectionView 不显示单元格图像

Xcode 5.1.1 上的 ios 7.0 模拟器

ios - NSURL 用冒号打破端口字符串

ios - 如何使用 Contacts with Swift 对联系人进行排序

ios - Swift:继承 ViewController 并添加目标?