yesthisisjoe
yesthisisjoe

Reputation: 2035

iOS and Swift: Sharing code between View Controllers

I know I will need a good amount of code to be shared throughout my app's different view controllers and I'm not sure the best way of organizing my code. For instance, I know that both view controllers will rely heavily on a particular structure, but I don't know where to declare this structure. My first guess would be to create some kind of file that would be imported by both ViewControllers, but I don't know whether this is the right way to go or not.

I'm still a little new to Swift and iOS programming and any help would be appreciated. Thanks!

Upvotes: 0

Views: 867

Answers (1)

Dimitry
Dimitry

Reputation: 6603

You may be referring to some common business logic both of your controllers need to access. This can be done by having business logic classes that you either instantiate by each controller or using a singleton helper class (just don't store state in the singleton!).

Check out the MVC programming design pattern.

The controllers can then pass information between each other in the form of context (an object) or an identifier to an object (e.g. an ID in a database table) and have the receiving controller call the related business objects to retrieve the necessary information.

Upvotes: 1

Related Questions