Search code examples
iosestimote

Setting value on ViewController class


I'm new to iOS development, but I'm trying to modify the example app provided by Estimote (I'm working with their Indoor Positioning SDK). In the example, there is a ViewController class called MenuViewController that contains a function called -loadLocationFromJSON(). Now, inside this function they create a new instance of a ViewController called locationViewController which gets pushed to the navigationController. So far, that all works fine and the location file builds correctly and is displayed on screen.

Now what I want to do is to create a switch which is located in the MenuViewController class which can toggle an attribute on the LocationViewController. The locationViewController has a property of its indoorLocationView called rotateOnPositionUpdate which you can set to true or false. Right before I push the locationViewController, I'd like to set that property according to the switch in my MenuViewController class. Here's what I have so far:

@IBAction func loadLocationFromJSON() {
    let bundle = NSBundle.mainBundle()
    let path = bundle.pathForResource("location", ofType: "json")
    let content = NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil) as String
    let location = ESTLocationBuilder.parseFromJSON(content)
    var locationViewController:LocationViewController = LocationViewController(nibName: "LocationViewController", bundle: nil)
    locationViewController.location = location
    // the following line is where the code fails
    locationViewController.indoorLocationView.rotateOnPositionUpdate = false

    self.navigationController?.pushViewController(locationViewController, animated: true)
}

As you no doubt noticed, right now I'm just trying to set the property to false (and not even worry about the state of the switch)... but this fails when I try to build it. The debugger returns an error saying "fatal error: unexpectedly found nil while unwrapping an Optional value". I don't entirely know what this means and I don't know why I can't set the property of that locationViewController class. Can someone provide some help?


Solution

  • ViewControllers do not construct their views until they are first requested. Usually, this is during the pushViewController step, but it doesn't have to be.

    Additionally, It's likely that they have declared indoorLocationView as optional because it is a view and they construct it when the rest of the view is loaded. You'll need to either check for nil explicitly or use the ? operator to avoid crashes when accessing properties that might be nil.

    locationViewController.indoorLocationView?.rotateOnPositionUpdate = false
    

    When indoorLocationView is nil, this line will not crash, but it will also have no effect. You should either change the implementation of LocationViewController to accept this parameter directly and then pass it on to its indoorLocationView or set it after you've called pushViewController.