Pass Variables to a new View Controller via a Subclass and Dozens of Map Pins

后端 未结 1 1472
清酒与你
清酒与你 2020-12-07 04:45

I have a few moving parts in this one that I can\'t seem to stitch together, hopefully it is pretty straightforward.

Previous questions don\'t use a subclass and in

相关标签:
1条回答
  • 2020-12-07 05:34

    It seems that you're very close. In calloutAccessoryControlTapped, you're get getting the place name and info. I'm assuming that's what you want to pass to the second view controller, so go ahead and do so before you show it:

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        let capital = view.annotation as! Capital
        let placeName = capital.title
        let placeInfo = capital.info
    
        let secondViewController = sUIKeyInputUpArrowtoryboard!.instantiateViewController(withIdentifier: "SecondViewController") // I'm not sure why you're not just doing `storyboard.instantiateViewController(...); do you really have multiple storyboards floating around?
        secondViewController.placeName = placeName
        secondViewController.placeInfo = placeInfo
        show(secondViewController, sender: self)  
    }
    

    That presumes, of course, that your second view controller is has those placeName and placeInfo properties, e.g.

    class SecondViewController {
    
        var placeName: String!
        var placeInfo: String!
    
        override func viewDidLoad() {
            // use placeName and placeInfo to populate UI controls as necessary
        } 
    }
    

    I confess, though, that your question has a ton of unrelated code that's hard to make sense of, so it's not clear precisely what you need to do. But the idea is clear, that calloutAccessoryControlTapped should

    • figure out what needs to get passed to the next view controller;
    • instantiate that view controller;
    • set the appropriate properties in that next view controller;
    • then show it; and
    • that second view controller should use whatever properties you set in the preceding view controller to configure it's UI.

    Note, calloutAccessoryControlTapped in the first view controller cannot update the UI controls in the second view controller directly (since the controls for that view controller have not yet been hooked up to the outlets in the storyboard), but rather just passes whatever data that second view controller needs. Then that second view controller will configure its controls in its viewDidLoad.

    0 讨论(0)
提交回复
热议问题