在展开可选值

时间:2015-06-17 05:04:30

标签: ios swift uitabbarcontroller mapkit optional

首先,我要感谢您的兴趣。非常感谢您给予的任何帮助,因为我在谷歌搜索一小时后就被困住了。

切换到标签栏控制器后,我的mkMapView返回nil,标题中出现错误。这对我来说没有意义,因为处理mkMapView的同一个类在以前工作正常。由标签栏管理的所有其他视图控制器按预期工作。当我尝试设置委托时发生错误(因为mapView似乎总是返回nil)。我已经尝试删除mapView并创建一个新的插座连接,但这也不起作用。

这是控制台中发布的错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

感谢您提供的任何帮助。

这是我更改为包含自定义标签栏控制器的类:

 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


    }

这是旧的app委托:

 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()

这是管理viewcontroller和mapview的类(没有做任何更改,但这是xCode抛出错误的地方(这是唯一有问题的视图控制器):

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

1 个答案:

答案 0 :(得分:1)

之前我遇到过同样的问题。

@IBOutlet weak var mapView: MKMapView!更改为@IBOutlet var mapView: MKMapView!

因为mapView的弱变量有时会让它崩溃。

我还实施了一个检查,我看看搜索结果是否返回

if localSearchResponse == nil || self.mapView == nil{
                var alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
                alert.show()
                return
            } 

这些东西让它对我有用。我不知道为什么。但是我认为如果搜索需要很长时间才能完成/执行它会同时松开mapView,因为它的弱变量