从另一个函数访问我的mapView var:mapView = nil

时间:2016-02-22 01:00:41

标签: ios swift

我正在尝试从一个函数在mapView上放置一些注释,但mapView在调试器中显示为nil。我从另一个类调用下面的函数,调用似乎通过设置一些断点来工作。它只是在尝试将注释数组添加到mapView上时出错。

let temp = MapViewController()
temp.createCurrentSevereAnnotations()

这是我的MapViewController代码:

import UIKit
import MapKit

final class MapViewController : UIViewController, MKMapViewDelegate{

@IBOutlet weak var mapView: MKMapView!

    // Change the Map Type from the tool bar segmented button.
//this function works fine.  mapView is not nil.
@IBAction func changeMapType(sender: UISegmentedControl) {
    switch (sender.selectedSegmentIndex) {
    case 0:
        mapView.mapType = .Standard
    case 1:
        mapView.mapType = .Hybrid
    default:
        mapView.mapType = .Satellite

    } //^ this func works fine
 }
    //Puts an array of CustomPointAnnotation on the map.
 func createCurrentSevereAnnotations() {
    var annotations: Array = [CustomPointAnnotation]()
    let severeReports = currentSevereReports

    for item in severeReports
    {
        let annotation = CustomPointAnnotation(latitude: Double(item.lat!), longitude: Double(item.lon!))
        print("The lat is: \(item.lat!)")
        print("The lon is: \(item.lon!)")
        switch(Int(item.type!))
        {
        case 1:
            annotation.imageName = "ef0"
            annotation.type = "Tornado"
            break
        case 2:
            annotation.imageName = "wind"
            annotation.type = "Wind"
            break
        default:
            annotation.imageName = "hail"
            annotation.type = "Hail"
            break
        }
        print("item type : \(annotation.type)")
        annotation.title = item.magnitude
        annotation.subtitle = item.location
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = .MediumStyle
        annotation.time = item.time
        annotation.county = item.county
        annotation.remarks = item.remarks

        annotations.append(annotation)
    }
    print("Annotations count = \(annotations.count)") //<- works
    mapView.addAnnotations(annotations) //<- this is where mapView is nil
 }


    // The class for our CustomPointAnnotation
 class CustomPointAnnotation: NSObject, MKAnnotation {
    var title: String? //mag
    var subtitle: String? //location
    var latitude: Double
    var longitude:Double
    var imageName: String!
    var time: String?
    var remarks : String?
    var county: String?
    var type: String?
    var comments: String?


    var coordinate: CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    }

    init(latitude: Double, longitude: Double) {
        self.latitude = latitude
        self.longitude = longitude
    }
 }
}

2 个答案:

答案 0 :(得分:1)

问题出在这里。

let temp = MapViewController()

这是实例化视图控制器的方式。

您应该使用故事板中的加载。 (click here to check apple documentation)

func instantiateViewControllerWithIdentifier(_ identifier: String) -> UIViewController

尝试这种方式来实例化temp。

let temp = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MapViewControllerIdentifier") as UIViewController
self.presentViewController(temp, animated: true, completion: nil)

您应该为视图控制器定义Identifier (我使用&#34; MapViewControllerIdentifier &#34; in我的示例),您可以在应用的storyboard视图中执行此操作。

答案 1 :(得分:0)

尝试以不同方式实例化MapViewController。您可以像这样在Storyboard中实例化:

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)

let mapVC =  mainStoryboard.instantiateViewControllerWithIdentifier("mapVC") as! MapViewController

您需要进入故事板以设置“mapVC”ID。选择您的MapViewController,进入Identity Inspector(从右侧的Utilities面板)并在那里设置Storyboard ID。

然后你可以呈现控制器(如果你想):

presentViewController(mapVC, animated: true, completion: nil)

MapView为零是正常的,因为它来自故事板。当您的控制器具有来自IB的控件或视图时,您必须实例化故事板。

一般来说,当在Storyboard中创建ViewController时,您需要从Storyboard中调用。