Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

ios - Swift IBOutlet is nil but it is connected storyboard

I'm developing an iOS application using SwiftUI, but I need to develop with UIKit some features what there are no supported in SwiftUI. So, I'm developing UIKit in SwiftUI, but UIKit's IBOutlet is nil so I can't update UIKit value. Is there any way to declare IBOutlet manually?

class StoreDetailViewController : UIViewController, CLLocationManagerDelegate, updateValue{
    @IBOutlet weak var mapView: NMFMapView!
    var lat : Double = 0.0
    var lon : Double = 0.0
    let marker = NMFMarker()

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        
        view.addSubview(mapView)
    }
    
    @IBSegueAction func loadSwiftUI(_ coder: NSCoder) -> UIViewController? {
        return UIHostingController(coder: coder, rootView: storeDetail())
    }
    
    func removeMarker(){
        marker.mapView = nil
    }
    
    func setMarker(lat: Double, lon: Double){
        print("setting marker, ", lat, lon)
        marker.position = NMGLatLng(lat: lat, lng: lon)
        marker.mapView = self.mapView

    }
}

struct loadViewController: UIViewControllerRepresentable{
    var delegate : updateValue?

    func updateUIViewController(_ uiViewController: StoreDetailViewController, context: Context) {

    }
    
    func setPosition(latitude: String, longitude: String){
        print("position : ", latitude, longitude)
        self.delegate?.setMarker(lat: Double(latitude)!, lon: Double(longitude)!)
        StoreDetailViewController().setMarker(lat: Double(latitude)!, lon: Double(longitude)!)
        delegate?.setMarker(lat: Double(latitude)!, lon: Double(longitude)!)
    }
    
    typealias UIViewControllerType = StoreDetailViewController

    func makeUIViewController(context: UIViewControllerRepresentableContext<loadViewController>) -> UIViewControllerType {
        UIStoryboard(name: "storeDetail", bundle: nil).instantiateViewController(identifier: "Store Detail View Controller") as! StoreDetailViewController
    }
}
question from:https://stackoverflow.com/questions/65713796/swift-iboutlet-is-nil-but-it-is-connected-storyboard

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When instantiating your view controller in makeUIViewController, the outlets haven't been initialised yet.

Any updates you would like to make should be done with updateUIViewController:

struct loadViewController: UIViewControllerRepresentable{
    typealias UIViewControllerType = StoreDetailViewController

    var lat: Double
    var lon: Double

    func makeUIViewController(context: UIViewControllerRepresentableContext<loadViewController>) -> UIViewControllerType {
        UIStoryboard(name: "storeDetail", bundle: nil).instantiateViewController(identifier: "Store Detail View Controller") as! StoreDetailViewController
    }

    func updateUIViewController(_ uiViewController: StoreDetailViewController, context: Context) {
        uiViewController.setMarker(lat: lat, lon: lon)
    }
}

From your SwiftUI code:

loadViewController(lat: 0.0, lon: 0.0)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...