How can i add label programatically, getting error

Hi,

currently doing this but getting fatal error -

How can i add label programatically, currently doing this but gets a fatal error, kindly guide import UIKit class QuizViewController: UIViewController
{


override func viewDidLoad() {
super .viewDidLoad()

}

override func loadView() {

let questionLabel = UILabel()
questionLabel.text = "Question"
questionLabel.frame = CGRect(x: 0, y: 0, width: 50, height: 100)
view.addSubview(questionLabel)

}


}

Kindly guide where i am making mistake, thanks

I just deleted and added a new viewcontroller and it works, strange solution

Hi @amitsrivastava,
in the old view controller, did you first try to create a label on the storyboard and connect it to the code, which then you deleted to create the same from code?

if so, then the reason for your error was an outlet that was set in Storyboard but did not exist in code.

cheers,

@jayantvarma thanks, it seems i was trying to add the label to the subview before there was any thing to add to , i deleted the files and added the code in viewdidLoad and it works …

This topic was automatically closed after 166 days. New replies are no longer allowed.