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

懵懂的女人 提交于 2019-11-28 02:23:06

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!