Swift 4 Kā pārsūtīt datus starp ViewControllers un ContainerViews?

Man ir 3 ViewControllers: ViewController A un ViewController B, un Controller C, kas faktiski ir ContainerView, kas sastāv no diviem UIView.

Mans stāstu panelis

Kā redzat iepriekš redzamajā attēlā, ViewController C ir skaidrs fons, tāpēc “Test Label” var redzēt gan ViewController A, gan B UIView.

Kad es velku uz augšu no ViewController A, lai pārietu uz ViewController B, es vēlos, lai varētu veikt kādu animāciju (izgaismot/izbalināt, tulkot, mainīt tekstu utt.). Pieņemsim, ka es vēlos mainīt tekstu no “Test Label” uz “Some new text”, problēma ir tāda, ka, tiklīdz es nokļūstu ViewController B, tiek parādīts kļūdas ziņojums “Negaidīti atrasts nulle, atslēdzot izvēles vērtību”.

Kāpēc tiek rādīts nulle un kā pareizi mainīt etiķetes tekstu?

Šķiet, ka šim kodam ir jēga, bet es to nevaru saprast:

let containerViewController = ContainerViewController()
        containerViewController.testLabel.text = "Some new text"

Esmu arī mēģinājusi:

let containerViewController = storyboard?.instantiateViewController(withIdentifier: "containerViewController") as! containerViewController


        containerViewController.testLabel.text = "Some new text"

Vai man ir kaut kas jāpievieno ViewController A ignorēšanas funkcijās sagatavot?


person TNasty    schedule 05.06.2019    source avots
comment
programmingios.net/dont-make-a-new-instance -kļūdas dēļ   -  person matt    schedule 05.06.2019


Atbildes (2)


Varat piešķirt iegulšanas secībai no konteinera uz viewController C identifikatoru, piemēram, embedSegueFromBToC, pēc tam noķert faktisko ViewController C, gatavojoties secībai vecāku kontrollerī, piemēram, B.

Tāpēc B viewController pievienojiet šo:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "embedSegueFromBToC" {
            if let viewControllerC =  segue.destination as? ViewControllerC {
                viewContrllerC.loadViewIfNeeded() // you might need this since the need to access the testLabel which a UI component that need to be loaded
                viewControllerC.testLabel.text = "Some new text"
            }
        }
    }
person Hossam Sherif    schedule 05.06.2019
comment
Piecēlos no gultas, lai to izmēģinātu.. liels paldies! Strādāja perfekti! - person TNasty; 05.06.2019

Pareizi, sākotnējā skata kontrollerī ir jāizmanto sagatavošana (for sece:..) ignorēšana, tā tiks nodota ViewController B instancei kā segue.destination.

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if let viewControllerB = segue.destination as? ViewControllerB
    {
        viewControllerB.testLabel.text = "Some new text"
    }
}

Lai iegūtu plašāku informāciju, skatiet šo pamācību: https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/

person ekscrypto    schedule 05.06.2019
comment
Ahh, tas ir jēga, tas ir tas, ko es biju domājis, bet tad Hossama atbilde visu noskaidroja. Paldies par tavu palīdzību! - person TNasty; 05.06.2019