Get attribute of viewcontroller created with storyboard

前端 未结 1 745
孤街浪徒
孤街浪徒 2021-01-29 00:55

I\'m currently learning swift and I\'m trying to learn of instanciation from storyboard works, but the error I\'m facing now isn\'t documented very much.

I created a vie

相关标签:
1条回答
  • 2021-01-29 01:22

    Right after instantiating the controller the outlets are not connected yet, you have to declare temporary variables and set the outlet properties in viewDidLoad()

    ...
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "NewsView") as! SimpleNewsViewController
        controller.tempImage = UIImage(named: "image.jpg")
        controller.tempLabel = "this is an example that can be really long"
        controller.tempText = "this is a title example
        self.navigationController?.pushViewController(controller, animated: true)
    ...
    

    class SimpleNewsViewController: UIViewController {
    
    @IBOutlet weak var myImage: UIImageView!
    @IBOutlet weak var myLabel: UILabel!
    @IBOutlet weak var myText: UITextView!
    
    var tempImage : UIImage?
    var tempLabel = ""
    var tempText = ""
    
    var event: Events!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        myImage.image = tempImage
        myLabel.text = tempLabel
        tempText.text = tempText
    }
    
    0 讨论(0)
提交回复
热议问题