简体   繁体   中英

How to access IBOutlet in UITableViewController Class if the IBOutlet is in UIViewController Class

在此处输入图片说明

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM