Andrew
Andrew

Reputation: 239057

How to pass data from a custom view to a controller in iOS?

I have a custom subview that is shared between controllers/views. I want to "bubble up" some data/event to the controller when the user takes an action in the custom view (i.e. tap a button, select an option). What is the correct way to do this in iOS? Note that I am not using Xcode nor Interface Builder. I would like to know how to do this in code.

Upvotes: 0

Views: 50

Answers (1)

Jelly
Jelly

Reputation: 4532

Usually you create a model to hold your data and you either create a delegate for your custom subview or use blocks/closures (the first approach is usually preferred). When the action occurs you "bubble up" the data in a model instance and call a delegate method or block depending on what you choose, with the model instance as a parameter. Usually the delegate is the view controller but you could also add some indirection if necessary to sparse things out (have a separate object handling the actions of your custom view).

Upvotes: 1

Related Questions