Giuseppe
Giuseppe

Reputation: 165

Is possibile to use the same view controller to show different content for each?

I explain my question: in my iOS app, written in Swift, I have a mapview, using mkmapkit, where i added my mkpoint annotations, seven in total. When i tap on each annotation callout, it opens a new view controller using performSegueWithIdentifier. My question is: Do I need to use seven different view controllers, one for each annotation callout tap, or can I use just one view controller? Because the view controller design must be the same for all annotation callout taps, but with different content data for each one.

Sorry for my bad english. I hope to figure out to this problem

Upvotes: 0

Views: 1097

Answers (3)

Pavel Gatilov
Pavel Gatilov

Reputation: 2590

Of course you should use only one ViewController. You just need to pass data from your MapViewController into ViewController through performSegue. And display it.

This pattern called Master-Detail and you can find more here:
An iOS 8 Swift Split View Master-Detail Example

Upvotes: 0

zisoft
zisoft

Reputation: 23078

When you perform the segue via performSegueWithIdentifier, prepareForSegue is called. Override this method to pass data to the destination ViewController:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "mySegueIdentifier" {
        let destinationViewController = segue.destinationViewController as MyDestinationViewController

        // pass data to destinationViewController
        destinationViewController.myProperty = ...
    }
}

Upvotes: 3

Graeme K
Graeme K

Reputation: 89

Use prepareForSegue to pass individual information to the same view.

Upvotes: 0

Related Questions