我有一个迅速创建的计算器。我正在尝试向其添加其他功能,我想将存储在historyArray中的已按下操作的历史记录传递到TableViewController中。我正在尝试隔离数据,但似乎无法获取。我为正在玩的segue方法准备了几种不同的方法,这些方法被注释掉了。到目前为止,这是我的代码:

ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var displayLabel: UILabel!
    @IBOutlet weak var HistoryLabel: UILabel!

    var historyArray: [String] = []
    var userIsTypingNumbers = false
    var firstNumber = 0
    var secondNumber = 0
    var operation = ""
    var result = 0.0

    @IBAction private func NumbersEntered(_ sender: UIButton) {

        //know what number is being pressed
        let number = sender.currentTitle
        //if user is typing number, do this.
        if userIsTypingNumbers {
            //specify what number is being pressed.
            //append the number onto the previous number.
            displayLabel.text = displayLabel.text! + number!
        } else {
            displayLabel.text = number
            userIsTypingNumbers = true
        }
    }
    var displayValue: Double {

        get {
            return Double(displayLabel.text!)!
        }
        set {
            displayLabel.text = String(newValue)
        }
    }

    private var calculations = PerformCalculations()


    @IBAction func OperationsPressed(_ sender: UIButton) {
        userIsTypingNumbers = false
        firstNumber = Int(Double(displayLabel.text!)!)
        operation = sender.currentTitle!
        if operation == "√" {
            result = (PerformCalculations().squareroot(a: Double(firstNumber)))
            displayLabel.text = String(result)
        }
    }


    @IBAction func Enter(_ sender: UIButton) {
        userIsTypingNumbers = false
        secondNumber = Int(Double(displayLabel.text!)!)


        if operation == "+" {
            result = (PerformCalculations().add(a: Double(firstNumber), b: Double(secondNumber)))
        } else if operation == "÷" {
            result = (PerformCalculations().division(a: Double(firstNumber), b: Double(secondNumber)))
        } else if operation == "×" {
            result = (PerformCalculations().multiplication(a: Double(firstNumber), b: Double(secondNumber)))
        } else if operation == "-" {
            result = (PerformCalculations().subtract(a: Double(firstNumber), b: Double(secondNumber)))
        }
        displayLabel.text = String(result)

        historyArray.append("\(firstNumber) \(operation) \(secondNumber) = \(result)")
        userIsTypingNumbers = false

        //self.performSegue(withIdentifier: "History", sender: self)

    }


    @IBAction func Clear(_ sender: UIButton) {
        //clear display to 0.
        displayLabel.text = "0"
    }

    @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? TableTableViewController {
                destinationVC.dataString = historyArray.description
            }
        }
    }*/

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destViewController : TableTableViewController = segue.destination as! TableTableViewController

        destViewController.dataString = historyArray.description

    }
}


模型模型

import Foundation


class PerformCalculations {

    func add(a: Double, b: Double) -> Double {
        let result = a + b
        return result
    }
    func division(a: Double, b: Double) -> Double {
        let result = a / b
        return result
    }
    func subtract(a: Double, b: Double) -> Double {
        let result = a - b
        return result
    }
    func multiplication(a: Double, b: Double) -> Double {
        let result = a * b
        return result
    }
    func squareroot(a: Double) -> Double {
        let result = sqrt(a)
        return result
    }
}


TableTableViewController.swift

import UIKit

class TableTableViewController: UITableViewController {

    //var dataString:String!

    var historyArray: [String] = []
    var i = 0


    override func viewDidLoad() {
        super.viewDidLoad()
        i = 0
        while i < historyArray.count{
            AddLabel(history: historyArray[i])
            i = i+1
        }

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

        func AddLabel(history: String){
        let label = UILabel()
        label.text = history
        let y = i*15
        label.frame = CGRect(x:125,y:y,width:200,height:21)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }

    /*
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


任何帮助将不胜感激,谢谢!

最佳答案

在您选择的视图控制器中,可以使用historyArray访问ViewController实例的(self.presentingViewController as? ViewController).historyArray

关于ios - 将historyArray值保存到UITableViewController中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43465105/

10-13 03:59