class AClass: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {
@IBOutlet var mapKitView: MKMapView!
}
class BClass: UITableViewController {
var localAClass=AClass()
localAClass.mapKitView.mapType=MKMapType.standard
}
Fatal error: Unexpectedly found nil while unwrapping an Optional value
In ClassB, you need to use UIStoryboard instantiateViewController(withIdentifier:)
to have IBOutlets instantiated.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController =
storyboard.instantiateViewController("AClassIdentifier") as!
AClass
And then still, IBOutlets views aren't instantiated until viewDidLoad() for that class. Your ClassA
would need a second property such as mapType
, and ClassB
sets that property instead of directly on the view. Then in ClassA viewDidLoad
you can set the actual mapView based on this stored property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.