Womble
Womble

Reputation: 5290

iOS Pattern for Tab Bar Controller: View Controllers that share functionality

I am using Storyboards, iOS 7 SDK, Xcode 6 Brta.

I have a UITabBarController.

It has two Storyboard Relationships:

(1) a Relationship to UINavigationController 1, in which is embedded View Controller 1.

(2) a Relationship to UINavigationController 2, in which is embedded View Controller 2.

Each View Controller has a common set of UI elements within each of their Navigation Bars. e.g. an Options button, a Refresh button, etc.

What these buttons do is the the same in each View Controller, but rely on the particular View Controller and its Storyboard settings.

For example:

The user presses the Options button [in the Navigation Bar] of View Controller 1, and Segue xx1 will occur, causing a transition from View Controller 1 to the Options View Controller.

or...

The user presses the Options button [in the Navigation Bar] of View Controller 2, and Segue xx2 will occur, causing a transition from View Controller 2 to the Options View Controller.

(In this case, each View Controller will cause the same Options UI to appear, but through different Segues.)

Clearly, this replicates functionality, introduces complexity and doesn't scale up.

How should I go about designing/refactoring this, such that the pressing of a button that appears in 2 or 3 or n View Controllers, has the same behavior in each, but without the duplication of code and Storyboard entanglement?

Is there a typical pattern for this? Cheers.

Upvotes: 0

Views: 218

Answers (1)

nburk
nburk

Reputation: 22731

One option would be to create a superclass for ViewController1 and ViewController2, where you implement the logic for the common behaviour (e.g. the options button) and specify the common design elements, in this case you wouldn't have redundant code and you could simply add more view controllers to the tab bar controller by subclassing again.

Upvotes: 0

Related Questions