1

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.

1
  • I have the same problem. I tried removing weak from @IBOutlet but that doesn't help. However all the non-IBOutlet vars (e.g. index, parent, etc) are accessible. It has to do with how these objects are instantiated (or not) from the storyboard/XIB file. Still digging. Commented Jul 16, 2015 at 17:58

2 Answers 2

4

Well, pageArray is probably nil and with ! you are pretending that it is not.

Instantiating pageArray should solve your issue.

You can check the first answer here to learn more about question and exclamation marks in swift

EDIT:

Your problem might also come from your controller initialization, you might want to try:

let mystoryboard:UIStoryboard = UIStoryboard(name: "storyboardName", bundle: nil) var newPage:Page = mystoryboard.instantiateViewControllerWithIdentifier("idYouAssigned") as! Page 
Sign up to request clarification or add additional context in comments.

12 Comments

That may be a problem, but it isn't the only one. I am getting the error before that, on this line: newPage.Label.text = quote
@Xebeq Do you have a xib file linked to PageView ? If so i believe it is not loaded and Label is always nil. You might want to use NSBundle.mainBundle().loadNibNamed. If you can confirm you have a xib file i'll detail my answer
Just realized, it is a viewcontroller. Init should be made with nib name. I've edited my answer.
I tried that before. For some reason, after I do this it thinks that newPage and pageArray are both strings (??). Deleting the next two lines and writing them again fixes this, but I still get the original error in the same place.
Also, the only .xib file in my project is LaunchScreen.xib and I do not believe it is linked to PageView.
|
0

I knew your problems. When you load a ViewController from Nib. Actually It's take a delay time until your ViewController has been loaded.

This code below help your ViewController load immediate.

@IBAction func StartButton(sender: AnyObject) { var pageArray: PageArray = PageArray() for quote in quoteList { var newPage = Page(nibName: "Page", bundle: nil) let _ = newPage.view // A trick. It's help your view load immediate // Your Page has been loaded. newPage.Label.text = quote pageArray.append(newPage) } 

3 Comments

This earns me another error: 2015-07-17 15:18:24.363 Quotes[3277:855732] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/user/Library/Developer/CoreSimulator/Devices/319D819D-C653-4AAF-ADDB-0514A8F055AE/data/Containers/Bundle/Application/042EE69B-E6D2-4CEF-A2D3-B1C438533DD1/Quotes.app> (loaded)' with name 'Page'' *** First throw call stack:
Do you try rename something like this? stackoverflow.com/questions/12722461/…
Well, nevermind. A combination of this and Justa's suggestion seems to have fixed it. No errors now, nothing's nil. Thanks! (time to move on to new exciting problems)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.