简体   繁体   English

Swift:无法使用发件人通过segue设置[String]

[英]Swift: Failing to set a [String] through a segue using the sender

Good day. 美好的一天。 I'm creating my first own app and have ran into an issue. 我正在创建自己的第一个应用程序,但遇到了问题。 I have a AR scene with clickable stuff which when you touch them a segue triggers into a ViewController and sets that view controllers labels and textview depending on what was touch on screen. 我有一个带有可点击内容的AR场景,当您触摸它们时,segue会触发到ViewController中,并根据屏幕上的触摸内容来设置视图控制器标签和textview。

Explanations: 1. CaseViewController is the target view controller. 说明:1. CaseViewController是目标视图控制器。 2. the "artNews" and "politicalNews" are string arrays in which I've written 3 strings, they are defined and are never nil. 2.“ artNews”和“ politicalNews”是我在其中编写了3个字符串的字符串数组,它们是定义的,从不为零。

Question: I get a crash due to the segueInputText being nil. 问题:由于segueInputText为零,导致崩溃。 Why does it become nil and how do I correct it? 为什么会变成零,我该如何纠正?

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let destinationVC = segue.destination as! CaseViewController
    destinationVC.segueInputText = sender as? [String]

    print("\(String(describing: sender))")
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    guard let touchLocation = touches.first?.location(in: sceneView),
        let hitNode = sceneView?.hitTest(touchLocation, options: nil).first?.node,
        let nodeName = hitNode.name
        else { return }

    if nodeName == imageNameArray[0] {

        performSegue(withIdentifier: "CaseViewController", sender: artNews)

    } else {
        print("Found no node connected to \(nodeName)")
        return
    }

    if nodeName == imageNameArray[1] {

        performSegue(withIdentifier: "CaseViewController", sender: politicalNews)

    } else {
        print("Found no node connected to \(nodeName)")
        return
    }

the CaseViewController has UILabels and UITextViews connected and this: CaseViewController具有连接的UILabels和UITextViews,并且:

    var segueInputText : [String]? {
    didSet {
        setupText()
    }
}

    func setupText() {

    // Why are these values nil? 
    testLabel.text = segueInputText![0]
    ingressLabel.text = segueInputText![1]
    breadLabel.text = segueInputText![2]

    testLabel.reloadInputViews()
    ingressLabel.reloadInputViews()
    breadLabel.reloadInputViews() //breadLabel is a UITextView
}

Thank you for reading my question! 感谢您阅读我的问题! Kind regards. 亲切的问候。

Remove didSet block as when you set the array inside prepare , observe triggers and the lbl is still nil 删除didSet块,就像将数组设置在prepare内一样,观察触发器,并且lbl仍然为nil

OR 要么

func setupText() {
   if testLabel == nil { // if one is nil then all of them too
     return 
   }
 }

Don't use the didSet observer in this case. 在这种情况下,请勿使用didSet观察器。 It will never work. 它永远都行不通。

In setupText() IBOutlets are accessed which are not connected yet at the moment prepare(for is called. setupText()中,访问尚未prepare(for setupText() IBOutlets prepare(for被调用。


Remove the observer 删除观察者

var segueInputText : [String]?

and call setupText in viewWillAppear 并在viewWillAppear调用setupText

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    setupText()
}

At the very moment when you do this: 在执行此操作的那一刻:

destinationVC.segueInputText = sender as? [String]

the destination view controller has not yet loaded hence none of the outlets are connected so accessing any of them will crash your app as they are still nil. 目标视图控制器尚未加载,因此没有连接任何插座,因此访问它们中的任何一个都将使您的应用程序崩溃,因为它们仍然为零。

You will have to assign any of the values you'd like to pass to the destination controller to a property and assign this property's value to the the corresponding outlet in viewDidLoad . 您将必须将要传递给目标控制器的任何值分配给一个属性,并将该属性的值分配给viewDidLoad的相应出口。 This way you make sure all outlets have connected. 这样,您可以确保所有插座均已连接。

For the same reason don't use a property observer to assign the property's value to any of the labels as this would happen, again, before the view controller had a chance to load… 出于同样的原因,请不要使用属性观察器将属性的值分配给任何标签,因为在视图控制器有机会加载之前,这种情况会再次发生……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM