0

I'm new in iOS development,

I have and "API Helper" Swift class that gets some data as a JSON array. And when the array is ready I want to call a method in my MasterViewController to update the tableView with the data.

I tried to do like that:

var facilities : [Facility]? {
        didSet {

            MasterViewController().facilitiesLoaded()
        }
    }

And then reload the tableView but without seeing anything.

I think the problem is that I'm creating a new instance of the ViewController, but what I should have is to get access to the current instance of the class.

Any idea, or a better design? Thanks..

2
  • 2
    You are creating a new object and invoking a method in it, not calling your existing MasterViewController. Maybe a better design would be to either use a callback block or use NSNotifications. Commented Jun 16, 2015 at 13:02
  • Can you please give me a good example in Swift (or a link to) about the callback block, thanks.. Commented Jun 16, 2015 at 13:21

2 Answers 2

1

If this "facilities" variable is instance of MasterViewController then do :

var facilities : [Facility]? {
didSet {
        self.facilitiesLoaded()
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

No, it's not, I'm trying to set all data sources in one separate and shared class.
1

Thanks to @dcestari,

I did added callback blocks to the API call and handled it in the ViewController and that did the trick

In API caller method:

func loadFacilities(completionHandler:(() -> Void!)) {
    // do stuff
    completionHander()
}

In the ViewController:

func getFacilities() {
        api.loadFacilities({
             // update tableView
        })
    }

Comments

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.