IBOutlet of another view controller is nil

后端 未结 1 1434
渐次进展
渐次进展 2020-12-06 21:17

I have a storyviewcontroller which has objects on its view. I need to change the text on the UILabel(In the storyviewcontroller) and the load the view on an array. I have co

相关标签:
1条回答
  • 2020-12-06 21:43

    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)
    
    0 讨论(0)
提交回复
热议问题