1

I am passing data from one class to another, and I am receiving nil values when I breakpoint and look at the console.

This is the class with the values, and I want to pass these values to the class viewc

func viewControllerAtIndex(index: Int) -> viewc {
    if ((self.namepage.count == 0) || (index >= self.namepage.count) {
        return viewcard()           
    }

    var vc: viewc = self.storyboard?.instantiateViewControllerWithIdentifier("ItemController") as! viewc

    vc.imageFile = self.pageImages[index] //not nil
    vc.cTitle = self.namepage[index]      //not nil
    vc.nTitle = self.numberpage[index]    //not nil
    vc.pageIndex = index
    vc.getArray()

    return vc
}

This class is the class I want to pass the data to.

class viewc: UIViewController{
    var pageIndex: Int!
    var cTitle: String!
    var imageFile: String!
    var nTitle: String!

    @IBOutlet weak var imagen: UIImageView!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var number: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func getArray() {
        self.imagen.image = UIImage(named: self.imageFile)//imageFile is nil
        self.name.text = self.cTitle//cTitle is nil
        self.number.text = self.nTitle//nTitle is nil
    }
}
Eendje
  • 8,815
  • 1
  • 29
  • 31
stackerleet
  • 518
  • 1
  • 5
  • 16
  • The function should look something like this. – Harshal Bhavsar Feb 27 '16 at 06:02
  • Is `ItemController ` the name of your destination viewController? – Rashwan L Feb 27 '16 at 06:07
  • So I have a page view controller and ItemController is the Storyboard id of the viewController with the actual objects in the page, so yes it is the destination viewController. – stackerleet Feb 27 '16 at 06:08
  • @stackerleet: Have you connected your outlets properly? It is possible that they are not connected and therefore are being deallocated as soon as they're set (owing to their `weak` nature). – Vatsal Manot Feb 27 '16 at 06:10
  • I went ahead and reconnected the outlets and it still crashes with the nil error on the first line of the getArray function. Even if the outlets were faulty that doesn't explain why imageFile, cTitle, and nTitle are nil when I breakpoint and read it in the console. – stackerleet Feb 27 '16 at 06:16
  • It's in the code I posted above – stackerleet Feb 27 '16 at 06:31
  • The problem is the view hasn't loaded yet when you're accessing `getArray()`, so your `IBOutlet's` are still `nil`. Accessing will needlessly to say cause a crash. – Eendje Feb 27 '16 at 06:36
  • That's what I was thinking, but I wasn't sure how to fix it – stackerleet Feb 27 '16 at 06:37
  • Can I force the viewcontroller to load? http://stackoverflow.com/questions/29321383/iboutlet-is-nil-but-it-is-connected-in-storyboard-swift inside the array? – stackerleet Feb 27 '16 at 07:33
  • You should generally avoid forcing to load the view, but accessing the view will load the view. – Eendje Feb 27 '16 at 07:35
  • I think for the time being this is the best option. How would I force the controller to load I tried following that thread but I can't seem to do it. – stackerleet Feb 27 '16 at 07:36
  • 1
    Well, I think `print(vc.view)` will load the view. – Eendje Feb 27 '16 at 07:37
  • I did it like this and I still get the same error http://puu.sh/nn4Hm/25a9348531.png http://puu.sh/nn4MD/df57044c2c.png – stackerleet Feb 27 '16 at 07:39
  • 1
    Access the view first ;) Or else the results will be the same. – Eendje Feb 27 '16 at 07:41
  • Yup that did the trick! :) – stackerleet Feb 27 '16 at 07:45

2 Answers2

0
func viewControllerAtIndex(index: Int) -> ViewC {
    var vc = self.storyboard?.instantiateViewControllerWithIdentifier("ItemController") as! ViewC

    vc.imageFile = self.pageImages[index]
    vc.cTitle = self.namepage[index]
    vc.nTitle = self.numberpage[index]
    vc.pageIndex = index

    vc.testProperties()

    return vc
}

class ViewC: UIViewController{
    var pageIndex: Int!
    var cTitle: String!
    var imageFile: String!
    var nTitle: String!

    @IBOutlet weak var imagen: UIImageView!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var number: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        imagen.image = UIImage(named: imageFile)
        name.text = cTitle
        number.text = nTitle
    }

    // This will crash if one of them is nil
    func testProperties() {
        print("pageIndex: \(pageIndex)")
        print("cTitle: \(cTitle)")
        print("imageFile: \(imageFile)")
        print("nTitle: \(nTitle)")
    }
}

Now your ViewC will be set up properly. Notice you can only access your IBOutlet after the view has loaded. If you need to access the outlets before the view has loaded, you'll need to do something else.

Eendje
  • 8,815
  • 1
  • 29
  • 31
  • If I take this route then imageFile, cTitle, and nTitle will be nil – stackerleet Feb 27 '16 at 06:45
  • That depends on where you're accessing them. Somehow I'm suspecting you're not accessing the same instance of `ViewC`. – Eendje Feb 27 '16 at 06:48
  • I tried it and when I do my original approach cTitle,nTitle, and imagen have values, but when I take your approach everything is nil. – stackerleet Feb 27 '16 at 06:49
  • To be honest, this doesn't differ much from your original approach though :). Let me update the answer with some checks. – Eendje Feb 27 '16 at 06:56
  • Can I force the viewcontroller to load? http://stackoverflow.com/questions/29321383/iboutlet-is-nil-but-it-is-connected-in-storyboard-swift inside the array? – stackerleet Feb 27 '16 at 07:34
  • Still, if my answer doesn't work. Then I'm pretty sure it's because of something else (probably because you're not accessing the same instance). – Eendje Feb 27 '16 at 07:36
-2

The function should look like this.

func viewControllerAtIndex(index: Int) -> UIViewController? 
{
    let vc = storyboard?.instantiateViewControllerWithIdentifier (pagesArray[index]) return vc 
} 
PT Vyas
  • 722
  • 9
  • 31
Harshal Bhavsar
  • 1,598
  • 15
  • 35