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.

Xebeq
  • 95
  • 1
  • 10
  • 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. – AMC08 Jul 16 '15 at 17:58

2 Answers2

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
Community
  • 1
  • 1
streem
  • 9,044
  • 5
  • 30
  • 41
  • 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 Jul 16 '15 at 17:20
  • @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 – streem Jul 16 '15 at 21:02
  • Just realized, it is a viewcontroller. Init should be made with nib name. I've edited my answer. – streem Jul 16 '15 at 21:10
  • 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. – Xebeq Jul 17 '15 at 08:07
  • Also, the only .xib file in my project is LaunchScreen.xib and I do not believe it is linked to PageView. – Xebeq Jul 17 '15 at 08:18
  • @Xebeq Do you have a storyboard ? Besides how do you initialize pageArray ? – streem Jul 17 '15 at 08:26
  • 1.Yes, I do; 2.See Edit to original post. – Xebeq Jul 17 '15 at 10:26
  • @Xebeq I've edited my answer to instantiate a controller from a storyboard correctly. Please make sure that the Label is correctly linked in the storyboard to your IBOutlet as well. – streem Jul 17 '15 at 10:42
  • Where would I see the ViewController's ID? – Xebeq Jul 17 '15 at 12:23
  • @Xebeq in the storyboard, if you click on your controller, in the right panel you can set the id. – streem Jul 17 '15 at 12:31
  • Found it, but it doesn't help. Label is still nil :( – Xebeq Jul 17 '15 at 12:37
  • @Xebeq Have you linked it in storyboard ? – streem Jul 17 '15 at 12:56
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)

}
Long Pham
  • 7,464
  • 3
  • 29
  • 40
  • 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 (loaded)' with name 'Page'' *** First throw call stack:** – Xebeq Jul 17 '15 at 12:21
  • Do you try rename something like this? http://stackoverflow.com/questions/12722461/nsinternalinconsistencyexception-reason-could-not-load-nib-in-bundle-nsbun – Long Pham Jul 17 '15 at 12:26
  • 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) – Xebeq Jul 17 '15 at 13:05