0

I have a tabViewController with two view controllers, a map and a UIListView. I want to pass the current map region center to the listView controller but currently that requires the user to tap the UIListView controller first, then go back to the map, and then back to the listview controller in order for the map region center value to be passed.

Im sure there is a simple and elegant way of solving this, I'm just not sure how.

2 Answers 2

4

Why don't you create a singleton that gets updated by some view controllers and read by others? That would be the preferred way to let two otherwise unrelated view controllers communicate, even when not allocated at the same time.

Edit

Suppose you have a class like this:

class PointSingleton {
    var point = CGPointZero
    static let sharedPoint : PointSingleton = PointSingleton()

    private init() {}
}

You now can

PointSingleton.sharedPoint.point = CGPoint(x: 2, y: 3)

as well as use

PointSingleton.sharedPoint.point

wherever you like.

Sign up to request clarification or add additional context in comments.

4 Comments

That sounds like a great idea, but I'm not really sure how to accomplish it. If you could provide any guidance there that would be much appreciated.
I edited my answer to include some guidance. If that and googling swift singleton does not help, feel free to ask another question:)
Thanks! However, I am either failing at Google-fu or I have failed to understand the limitations of a singelton. But can I create a singelton class with functions to both set and retrieve a CGPoint so that the Map ViewController can set the map center and the list view controller can retrieve it?
I made another edit to make quite clear how to use a singleton.
0

On tap of UIListView item change the selected index of the Tab Bar Controller. Without any code provided, the best I can provide is

  tabBarController?.selectedIndex = 0;

then to pass data

var destinationViewController = tabBarController.viewControllers[0] as UIViewController
destinationViewController.whateverVar = "some value"

EDITED:

on your list view create a way to access your MapViewController

//your map
    var mapVC = tabBarController.viewControllers[0] as UIViewController

then grab the map center like you normally would

var centerCoord = mapVC.mapView.centerCoordinate

2 Comments

I think you slightly misunderstood my question which is totally my fault because it's not exactly very precise. But the issue is that I want to pass the map region center from the MapView controller to the ListView controller without showing the ListView controller or requiring the user to actively select it.
Thanks for the suggestion, however I found Jan Greve's solution to be a bit more elegant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.