0

I have two viewControllers that have segues to the same mapViewController. So I tried to change the tint color of a toolbar button on the map but of course it said error nil. Is there another way to do this. Because I need to tell the compiler that if the segue is coming from the tableViewController then make the bar button white "As if not selected" and if the segue was sent from the mainViewController then make the bar button yellow.

This is the tableView code

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "locationView"{

        let mapVC:MapViewController = segue.destinationViewController as! MapViewController
        mapVC.pinCoordinate = coordinate
        mapVC.snippetTitle = caseTitleOnLocation
        mapVC.snippetDescription = caseDescriptionOnLocation
        mapVC.addLocation.tintColor = UIColor.whiteColor()  //error

This is the mainViewController code

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "rescueMap"{
        let mapVC:MapViewController = segue.destinationViewController as! MapViewController
        mapVC.addLocation.tintColor = UIColor.yellowColor()  //error
    }

2 Answers 2

1

The reason why you have an error is because you are trying to set a value on a view that doesn't exist YET.

You can create a UIColor() variable in mapViewController then pass the color you want

Ex.

In mapViewController

class mapViewController{

   var barColor : UIColor!

   override func viewDidLoad(){
     self.toolbar.barTintColor = barColor
   }
}

In mainViewController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "rescueMap"{
        let mapVC:MapViewController = segue.destinationViewController as! MapViewController
        mapVC.barColor = UIColor.yellowColor() 
    }

you'll get the idea.

1
  • Happy to help :) if your question is answered please mark it answered so others who have the same problem can easily find solution Commented Feb 1, 2016 at 16:23
0

That is the issue because you are using the bar that is not initialized in the view. So first initialize the map view/Toolbar to change the tint color.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.