gpt4 book ai didi

ios - 继续 : Display stack to second View Controller

转载 作者:行者123 更新时间:2023-11-29 01:10:13 25 4
gpt4 key购买 nike

我正在快速创建一个 RPN 计算器。当按下磁带按钮以转到第二个 VC 时,我需要代码帮助来查看我的所有计算。当按下磁带按钮时,它将显示输入的计算。第二个 View Controller 中的功能。我知道编程。我已经在我的第一个 VC 中实现了 prepareforsegue。我不知道如何将堆栈传递给 numberOfRowsInSection

计算器引擎

class CalculatorEngine :NSObject

{

var operandStack = Array<Double>()

func updatestackWithValue(value: Double)
{
self.operandStack.append(value)
}


func operate(operation: String) ->Double
{

switch operation
{
case "×":
if operandStack.count >= 2
{
return self.operandStack.removeLast() * self.operandStack.removeLast()

第二个 View

class SecondVCViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var array = [""]

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

cell.textLabel?.text = array[indexPath.row]

return cell

最佳答案

一个简单的解决方案可能是使用 Singleton pattern .这是 design pattern旨在用于最多只能有一个实例的对象。我假设 CalculatorEngine 类也是如此。

Swift 中的单例很简单:只需将以下内容添加到您的 CalculatorEngine 类中:

static let sharedInstance : CalculatorEngine = CalculatorEngine()

这将创建一个类级别属性,可以从应用程序中的任何位置轻松访问该属性 - 您可以通过以下方式获取sharedInstance:

让引擎 = CalculatorEngine.sharedInstance

这将允许您访问计算器引擎,而不必担心在各种 segue 方法中来回传递引用。

作为旁注,单例模式有时被视为反模式,因为在复杂的应用程序中,它可能会导致难以正确模拟类以进行测试,并且还会增加应用程序中可变全局状态的数量。但如果明智地使用,它没问题 - 当然它非常适合您的要求。它也融入了 Cocoa Touch -UIApplication.sharedApplication 是一个单例。

关于ios - 继续 : Display stack to second View Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35845692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com