【问题标题】:Call Method from other Class in Swift从 Swift 中的其他类调用方法
【发布时间】:2015-09-06 22:07:14
【问题描述】:

我有 2 个视图控制器类。我想从 ViewController 调用 ViewController2 的一个方法,但是控制台给了我这个错误:

致命错误:在展开可选值时意外发现 nil (lldb)

这是我在 ViewController 类中的方法:

class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {

    @IBAction func testButton(sender: AnyObject) {
        ViewController2().setText("It's a Test!")
    }
}

这是来自 ViewController2 的一些代码:

class ViewController2: UIViewController {

    @IBOutlet weak var directionsTextField: UITextView!


    func setText(var fieldText:String){
            directionsTextField.text = directionsTextField.text + "\n \n" + fieldText
    }
}

我很乐意为您提供任何帮助! :-)

提前谢谢你:)

编辑:

我也明白了:

线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)

关于 setText 方法... 好像directionsTextField 还没有可能(??)

【问题讨论】:

  • 为什么不使用委托模式?正是您想要在这里实现的是UIViewControllers 相关(segue 等)
  • 我该怎么做?我不知道如何使用委托模式,或者我只是不完全明白你的意思。

标签: ios xcode swift instantiation


【解决方案1】:

您已经非常接近自己弄清楚了。崩溃是因为您的 directionsTextField 尚不存在。作为一个 IBOutlet,在调用 viewDidLoad 之前它不会存在。你如何解决这个问题?

您为您的SecondViewController 更改您的API 并给它一个directionsText 属性。以这样的方式结束:

@IBOutlet weak var directionsTextField: UITextView!
var directionsText = ""

override func viewDidLoad() {
    super.viewDidLoad()
    directionsTextField.text = directionsTextField.text + "\n\n\(directionsText)"
}

在您的 FirstViewController 中更改您的 IBAction 以匹配 SecondViewController 的新 API:

@IBAction func testButton(sender: AnyObject) {
    let secondController = SecondViewController() // Felt a bit odd creating an instance without having a way of referencing it afterwards.
    secondController.directionsText = "It's a test"
    // Do what you want. (Perhaps pushing your secondController)
}

【讨论】:

  • 你的意思是directionsTextField.text = directionTextField.text + "\n\n(directionsText)",对吧?
  • 是的,那是其中之一。
  • 非常感谢您的帮助,我终于成功了。你写的方式在我正在使用的“真实”方法中不适用 1:1,所以我不得不添加一些“prepareForSegue”的东西,但我想我会再次尝试按照你的方式实现它。不知何故,directionsText 是空的/空白的......也许它在类加载时设置为“”?
  • 我会 +1。好答案
  • @T.BenjaminLarsen 我现在 1-uppped 你:D 差不多 1.5 年后,但仍然 :)
【解决方案2】:

当您尝试实例化 Second ViewController 时会发生错误。因为你需要实现init 方法。

例如:

public class FirstViewController : UIViewController {

   public override func viewDidLoad() {
    super.viewDidLoad()

    let secondviewController = SecondViewController().setText("YOURTEXT")

}

}

第二个 ViewController:

public class SecondtViewController: UIViewController {

public required init(coder aDecoder : NSCoder) {
    super.init(coder : aDecoder)
}

public init(nibName nibNameOrNil : String){
    super.init(nibName : nibNameOrNil, bundle: SBTVPayViewController.frameworkBundle())
}

public convenience init() {
    self.init(nibName: "MyNibName")
}

public func setText(texto : String) {

    directionsTextField.text = directionsTextField.text + "\n \n" + fieldText


}

}

当您尝试实例化 ViewController 时,第一个执行它的方法是 init

【讨论】:

  • 非常感谢您提供快速而好的答案,但我仍然以某种方式收到错误.. 致命错误:在展开可选值 (lldb) 时意外发现 nil
  • 尝试定义函数的 de 值不是可选的。 func setText(var fieldText:String!)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
相关资源
最近更新 更多