Reputation: 1
Let me begin by saying thanks for taking interest. Any aid you give is greatly appreciated as I've been stuck after an hour of googling.
After switching to a tab bar controller, my mkMapView is returning nil with the error in the title. This does not make sense to me, as the same exact class that handles the mkMapView was working fine before. All other view controllers managed by the tab bar work as intended. The error occurs when I try to set the delegate (as the mapView seems to always return nil). I have already tried deleting the mapView and creating a fresh outlet connection, but that also does not work.
This is the error posted in the console:
fatal error: unexpectedly found nil while unwrapping an Optional value
Thanks for any help you can provide.
This is the class I changed to include the custom tab bar controller:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.makeKeyAndVisible()
let EYBar:EYTabBar = EYTabBar(icons: ["ic_search_white_36pt","ic_star_white_36pt","ic_message_white_36pt","form", "ic_settings_white_36pt"])
let tab1:ListViewController = ListViewController()
let nav1:UINavigationController = UINavigationController(rootViewController: tab1)
let tab2:MapSearchVC = MapSearchVC()
let tab3:ViewController = ViewController()
tab3.view.backgroundColor = UIColor.darkGrayColor()
let tab4:ViewController = ViewController()
tab4.view.backgroundColor = UIColor.cyanColor()
EYBar.viewControllers = [nav1,tab2,tab3,tab4]
EYBar.EYTabbarView.backgroundColor = UIColor(red: 63/255, green: 81/255, blue: 181/255, alpha: 1.0)
EYBar.selectedView.backgroundColor = UIColor(red: 48/255, green: 63/255, blue: 159/255, alpha: 1.0)
self.window!.rootViewController = EYBar
return true
}
This is the old app delegate:
var storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MapSearchVC") as! MapSearchVC
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController
let rightViewController = storyboard.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
let mapSearchVC = storyboard.instantiateViewControllerWithIdentifier("MapSearchVC") as! MapSearchVC
let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)
leftViewController.mapViewController = nvc
let slideMenuController = SlideMenuController(mainViewController: nvc, leftMenuViewController: leftViewController, rightMenuViewController: rightViewController)
self.window?.rootViewController = slideMenuController
self.window?.backgroundColor = UIColor.grayColor()
self.window?.makeKeyAndVisible()
This is the class managing the viewcontroller and mapview (made no changes, but this is where xCode throws the error (this is the only view controller with an issue):
class MapSearchVC: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
var locationManager = CLLocationManager()
@IBOutlet weak var mapView: MKMapView!
var listings = [masterProperty]()
let actionButton = MKButton()
let navTitle = UILabel(frame: CGRectMake(65, -2.5, 70, 44))
let navMenuView = UIImageView(frame: CGRectMake(0, 0, 60, 44))
let navIcon = UIImage(named: "ic_menu_white_24dp")
override func viewDidLoad() {
super.viewDidLoad()
navTitle.text = "Search"
navTitle.textColor = UIColor.whiteColor()
navTitle.font = UIFont(name: "Roboto-Regular", size: 20)
self.navigationController?.navigationController?.navigationBar.addSubview(navTitle)
navMenuView.image = navIcon
navMenuView.contentMode = UIViewContentMode.ScaleAspectFill
//self.navigationController?.navigationController?.navigationBar.addSubview(navMenuView)
// Do any additional setup after loading the view, typically from a nib.
var authStatus = CLLocationManager.authorizationStatus()
locationManager.delegate = self
getAllListings()
mapView.addAnnotations(listings)
mapView.delegate = self
Upvotes: 0
Views: 841
Reputation: 8661
I had the same problem before.
Change @IBOutlet weak var mapView: MKMapView!
to @IBOutlet var mapView: MKMapView!
Since weak var for mapView sometimes get it to crash.
Also I implemented a check where I see if the search result returned nil
if localSearchResponse == nil || self.mapView == nil{
var alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
alert.show()
return
}
These things got it to work for me. I dunno why. But I think if a search takes to long to ferch/perform it will loose the mapView meanwhile since its a weak var
Upvotes: 1