我在Xcode中使用Swift创建了一个计算器。我想为它添加另一个功能,我已经有一个显示以前计算的历史标签。我想将该历史记录标签中显示的数据传递给TableViewController,并且对于每个计算,我的历史记录标签显示它将每个数据添加到我的TableViewController中。我知道我应该可以使用perform segue,但我不确定。到目前为止,这是我的代码:
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var displayLabel: UILabel!
@IBOutlet weak var HistoryLabel: UILabel!
var userIsTypingNumbers = false
@IBAction private func NumbersEntered(_ sender: UIButton) {
//know what number is being pressed
let digit = sender.currentTitle!
//if user is typing a number, do this.
if userIsTypingNumbers{
//specify what number is being pressed
let textCurrentDisplay = displayLabel.text!
//append the another number onto the previous number.
displayLabel.text = textCurrentDisplay + digit
} else {
displayLabel.text = digit
}
userIsTypingNumbers = true
}
var displayValue: Double {
get {
return Double(displayLabel.text!)!
}
set {
displayLabel.text = String(newValue)
}
}
private var calculations = PerformCalculations()
@IBAction func Enter(_ sender: UIButton) {
//perform the calculations
if userIsTypingNumbers{
calculations.setOperand(operand: displayValue)
userIsTypingNumbers = false
}
if let mathematicalSymbol = sender.currentTitle {
calculations.performOperation(symbol: mathematicalSymbol)
}
HistoryLabel.text = HistoryLabel.text! + String(sender.currentTitle!);displayValue = calculations.result
HistoryLabel.text = HistoryLabel.text! + String(displayValue)
}
@IBAction func Clear(_ sender: UIButton) {
//clear display to 0.
displayLabel.text = "0"
HistoryLabel.text = ""
}
@IBAction func Delete(_ sender: UIButton) {
//deleting last typed number, if user messed up.
let name: String = self.displayLabel.text!
//count number of characters.
let stringLength = name.characters.count
let substringIndex = stringLength - 1
displayLabel.text = (name as NSString).substring(to: substringIndex)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "History"){
if let destinationVC = segue.destination as? HistoryClassViewController{
destinationVC
}
}
}
}
我已经开始尝试准备segue功能,但不确定如何使其工作。任何帮助将非常感激。希望这是有道理的。这是我的计算器如何在Xcode中布局的图片:
答案 0 :(得分:0)
在您的HistoryClassViewController
字段中创建
var someData: Any!
然后:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "History" {
if let destinationVC = segue.destination as! HistoryClassViewController {
destinationVC.someData = something
}
}
}
然后执行segue,你想输入:
self.performSegue(withIdentifier: "History", sender: self)
希望有所帮助
答案 1 :(得分:0)
在 HistoryClassViewController类
中将字符串变量作为全局变量示例:
class HistoryClassViewController: UITableViewController{
var dataString:String!
//............Continue code
//............
现在在ViewController类中编写此方法
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "History"){
if let destinationVC = segue.destination as? HistoryClassViewController{
destinationVC.dataString = displayLabel.text!
}
}
}
最后在TableViewController中访问dataString值。