I get the error in the title while trying to change the text of a label which is part of the Page class (a subclass of UIViewController)
@IBAction func StartButton(sender: AnyObject) { for quote in quoteList { var newPage = Page() //error is on the next line: newPage.Label.text = quote pageArray!.append(newPage) } } }
and here is the Page class:
class Page : UIViewController{ var index: Int = 0 var parent: PageArray? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBOutlet weak var Label: UILabel! @IBAction func previousButton(sender: AnyObject) { } @IBAction func gotoListButton(sender: AnyObject) { } @IBAction func nextButton(sender: AnyObject) { } }
I am new when it comes to swift programming, and iOs in general; I apologise if a similar question has already been asked. I searched first, but didn't find a solution that worked for me. I suspect the problem is with the initialization of newPage, and tried doing it a few different ways, but I can't seem to get it right. My question is what exactly am I doing wrong, and how can I fix it?
EDIT: Got it working like this (by working I mean not crashing and doing nothing):
@IBAction func StartButton(sender: AnyObject) { var pageArray: PageArray = PageArray() for quote in quoteList { var newPage = Page(nibName: "Page", bundle: nil) if newPage.Label != nil { newPage.Label.text = quote } pageArray.append(newPage) } } It seems certain now that newPage.Label is nil.