【问题标题】:I keep on getting this Error as I am trying to change the label text from a different View当我尝试从不同的视图更改标签文本时,我不断收到此错误
【发布时间】:2019-02-21 12:23:49
【问题描述】:

感谢任何帮助。 我是 Ios 开发的新手,我正在尝试更改位于我的第一个初始视图控制器中的标签文本。当我按下第二个视图控制器中的按钮时,我希望此文本发生更改,该按钮与最初的视图控制器相连。

这是我的第一个视图控制器

import UIKit

protocol gameModeDelegate {
    func didTapChoice(test:String)

}

class ViewController2: UIViewController {
    var selectionDelegate:gameModeDelegate!

    @IBAction func chooseButton(_ sender: Any) {

        selectionDelegate.didTapChoice(test: "TEST")

        let selectVC = storyboard?.instantiateViewController(withIdentifier: "VC1") as! ViewController

        present(selectVC,animated: true,completion: nil)            

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

这是我在第二个标签所在的地方所做的

override func viewDidLoad() {
        super.viewDidLoad()
            let selectVC2 = storyboard?.instantiateViewController(withIdentifier: "VC1") as! ViewController2
            selectVC2.selectionDelegate = self

            winningLabel.isHidden = true
            winningLabel.center = CGPoint(x: winningLabel.center.x, y: winningLabel.center.y - 400)
      playAgainoutlet.isHidden = true
            playAgainoutlet.center = CGPoint(x: playAgainoutlet.center.x, y: playAgainoutlet.center.y + 400)
    }
extension ViewController: gameModeDelegate{
        func didTapChoice(test: String) {
            CommunicationLabel.text = test
        }



    }

到目前为止,我尝试了这两种方法,但一直收到此错误。

线程 1:致命错误:在展开可选值时意外发现 nil

【问题讨论】:

  • 这是因为视图还没有加载,你的标签此时将为零,所以它给出了错误

标签: ios swift mobile fatal-error


【解决方案1】:

你不应该使用这种方法来达到结果,你可以使用两种不同的方法来达到相同的结果。

1- 使用委托协议方法: 在 secondViewController 中你应该声明这样的协议

protocol applySelecction {
    func applyText(text: String)
}

并在类中声明一个像这样的变量。

var delegate: apply selection?

然后在按钮动作中

@IBAction func saveButtom(sender: UIButton){
    //print(selected)
    delegate?.applySelection(text: text) //text is the value select from UILAbel o the option the user select
    self.dismiss(animated: true, completion: nil)
}

然后在 firstViewController 像这样符合 applySelection 协议

class FirstViewController: UIViewController,applySelection{
func applyText(text: String){
   //update the UIlabel here 

2- 使用闭包。 在 secondViewController 中,您应该像这样添加一个新变量,

 var applyText: ((String) -> Void)?

然后在

@IBAction func saveButtom(sender: UIButton){
    self.applyText(text) //text is your text to update
}

并在 firstViewController 中为这样的 segue 重写做准备。

let vc = segue.destination as! fisrtViewController)
            vc.applyText = { [weak self] data in
                guard let self = self else {return}
                self.text = text //this is assigning the text to self-text supposing text is a UILabel in this viewController                    
            }

您可以尝试两种可能适合您的方法之一。

编辑

试试这个。

 class ViewController2: UIViewController {
        var selectionDelegate:gameModeDelegate!

    @IBAction func chooseButton(_ sender: Any) {

        selectionDelegate.didTapChoice(test: "TEST")
        //if segue is a show segue
        self.navigationController?.popViewController(animated: true)
       //else is a modal segue.
        dismiss(animated: true, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad() 
    }
}

PD。您不必显示视图堆栈中已经存在的视图控制器,只需将其解散即可。祝你好运

【讨论】:

  • 我尝试了您的第一种方法,甚至另一种方法,它只是拒绝工作
  • 对不起,我太弱了,我没有时间阅读 SO,但是看你的帖子我猜你错过了什么,为什么代表打电话。解释一下第一个ViewController,第二个。
猜你喜欢
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 2014-05-03
  • 1970-01-01
相关资源
最近更新 更多