Pass variables from one ViewController to another in Swift

前端 未结 3 1300
一整个雨季
一整个雨季 2020-12-02 00:45

I have a calculator class, a first ViewController to insert the values and a second ViewController to show the result of the calculation. Unfortunately I get a error called

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 01:03

    I think the issue here is, you are trying to set the UI component (here, its the label : myResultLabel)

    When segue is fired from first view controller, the second view has not yet been initialized. In other words, the UI object "myResultLabel" is still nil.

    To solve this, you will need to create a local string variable in second controller. Now, set that string to what you are trying to display, and finally, set the actual label in "viewDidLoad()" of the second controller.

    Best Regards, Gopal Nair.

提交回复
热议问题