4

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 connected the IBOutlet to the label in the storyviewcontroller.

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

I have created an object of the storyviewcontroller class and am able to access its variables. However, after creating the object of the storyviewcontroller, the IBOutlet is nil. Because of this, I get an exception saying found nil while unwrapping

let story:StoryViewController = StoryViewController()
story.textLabel.text = sampleText()

Can you please help me with this! Here is a link to the whole project https://github.com/abhishekagarwal2301/DTC Thanks

1 Answers1

5

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)
m.s.
  • 16,063
  • 7
  • 53
  • 88
Zell B.
  • 10,266
  • 3
  • 40
  • 49
  • Thanks for the prompt reply! Is it necessary to load the view controller or can i do this in the init function of the class? – Abhishek Agarwal Feb 26 '15 at 12:56
  • No you don't need the init function since view controller viewDidLoad method is always called automatically. – Zell B. Feb 26 '15 at 12:59
  • If Im not wrong, in obj-c you could just do initwithnibname... That however does not work here. Could you please tell me why. Thanks – Abhishek Agarwal Feb 26 '15 at 13:00
  • Sorry but I'm not familiar with objective-c as I started iOS development with Swift directly – Zell B. Feb 26 '15 at 13:02
  • Oh, okay! Thanks and Ill try doing what you said – Abhishek Agarwal Feb 26 '15 at 13:06
  • Ok let me know if its working or if you have any other problem – Zell B. Feb 26 '15 at 13:07
  • The problem still persists. The IBOutlet is nil even inside the viewdidload class StoryViewController: UIViewController { IBOutlet weak var textLabel: UILabel! IBOutlet weak var inspiredButton: UIButton! var labelText:String = "" override func viewDidLoad() { super.viewDidLoad() textLabel.text = labelText textLabel is nil and hence this gives an exception – Abhishek Agarwal Feb 26 '15 at 13:21
  • The problem is the way how you are trying to initialize your view controller. Check the edited answer – Zell B. Feb 26 '15 at 15:26
  • This is when you want to initialize the view but is not for a initialized view. – Reza Roshan Jan 13 '20 at 08:53