【问题标题】:Pass variables from one ViewController to another in Swift在 Swift 中将变量从一个 ViewController 传递到另一个 ViewController
【发布时间】:2014-07-25 11:55:59
【问题描述】:

我有一个计算器类,第一个 ViewController 插入值,第二个 ViewController 显示计算结果。不幸的是,如果我单击按钮,我会收到一个名为“无法打开 Optional.None”的错误。我知道它的语法有问题,但我不知道如何改进它。

第一个 Viewcontroller 中的按钮在情节提要中设置为“Segue: Show (e.g. Push)”,以便在被点击时切换到 secondViewController。

计算器类类似于:

class Calculator: NSObject {

    func calculate (a:Int,b:Int) -> (Int) {
        var result = a * b
        return (result)
    }
}

Viewcontroller 调用该函数,插入 a/b 并想要更改位于 secondviewcontroller 中的标签:

class ViewController: UIViewController {

@IBAction func myButtonPressed(sender : AnyObject) {
    showResult()
}

var numberOne = 4
var numberTwo = 7

var myCalc = Calculator()

func showResult () {
    var myResult = myCalc.calculate(numberOne, b: numberTwo)
    println("myResult is \(String(myResult))")
    var myVC = secondViewController()
    myVC.setResultLabel(myResult)
}

这里是secondViewController的代码

class secondViewController: UIViewController {

@IBOutlet var myResultLabel : UILabel = nil

func setResultLabel (resultValue:Int) {
    myResultLabel.text = String(resultValue)
}

init(coder aDecoder: NSCoder!)
{
    super.init(coder: aDecoder)
}

【问题讨论】:

  • 你有什么问题?
  • UIViewControllers 之间的变化如何?
  • 谢谢你们,我在第一篇文章中添加了信息。我刚刚意识到我必须创建我的类的一个实例,现在称为“myVC”。不幸的是,我现在收到另一个名为“Can't unwrap Optional.None”的错误。
  • showResult()secondViewController 的值从何而来?是全局变量吗?它看起来不像 ViewController 类中的属性。在调用setResultLabel() 之前,您可以使用println()(或调试器)查看它的值吗?
  • 创建一个自定义初始化并将其传递到那里...?

标签: swift


【解决方案1】:

在 Swift 中,默认情况下一切都是公开的。 在类之外定义变量:

import UIKit

var placesArray: NSMutableArray!

class FirstViewController: UIViewController {
//
..
//
}

然后访问它

import UIKit

class TableViewController: UITableViewController {
//
placesArray = [1, 2, 3]
//
}

【讨论】:

  • 非常感谢。将字符串从 NSViewController 传递给 Popover 的 NSView 我快疯了,你的解决方案救了我。
【解决方案2】:

这里的问题是FirstViewController 没有引用SecondViewController 的实例。因此,这一行:

secondViewController.setResultLabel(myResult)

什么都不做(除了可能导致Can't unwrap Optional.None 错误)。有几种方法可以解决这个问题。如果您使用故事板 segues,您可以使用UIViewController-prepareForSegue 方法。这是一个例子:

在 FirstViewController 中:

override func prepareForSegue(segue: UIStoryboardSegue!,sender: AnyObject!){         
  //make sure that the segue is going to secondViewController
  if segue.destinationViewController is secondViewController{
    // now set a var that points to that new viewcontroller so you can call the method correctly
    let nextController = (segue.destinationViewController as! secondViewController)
    nextController.setResultLabel((String(myResult)))
  }
}

注意:此代码不会按原样运行,因为该函数无法访问结果变量。你必须自己弄清楚:)

【讨论】:

    【解决方案3】:

    我认为这里的问题是,您正在尝试设置 UI 组件(这里,它的标签:myResultLabel)

    当 segue 从第一个视图控制器触发时,第二个视图尚未初始化。也就是说,UI 对象“myResultLabel”仍然是 nil。

    要解决这个问题,您需要在第二个控制器中创建一个本地字符串变量。现在,将该字符串设置为您要显示的内容,最后,在第二个控制器的“viewDidLoad()”中设置实际标签。

    最好的问候, 戈帕尔奈尔。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多