IBOutlet of another view controller is nil

允我心安 提交于 2019-11-28 01:26:01
Zell B.

IBOutlets are initialized during view loading process and they are not accessible at the point you are trying to reach them. Instead you must declare a string variable to your viewcontroller and set text to label on its viewDidLoad method (after the loading process has finished)

class StoryViewController: UIViewController {
    @IBOutlet weak var textLabel: UILabel!
    @IBOutlet weak var inspiredButton: UIButton!
    var text:String!

   override func viewDidLoad() {
    super.viewDidLoad()

    textLabel.text = text
   }
}

And from first controller initialize text variable as follow

let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
var story = storyboard.instantiateViewControllerWithIdentifier("YourVCIdentifier") as StoryViewController
story.text = sampleText()
self.presentViewController(story, animated: false , completion: nil)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!