0

My app contains TabBar controller with 5 viewcontrollers. It is possible to click on a button in each of viewcontrollers which will popup another view in which user can choose a setting. The button (which was clicked) is supposed to change its background according to chosen setting in each viewcontroller. So if the user clicks on button in VC1 and chooses the setting, this information should spread into all of the other VCs so that the button has the same background.

I am using storyboards, and I know that this is easily possible between 2VCs using segues, protocols, closures... I cannot find a proper way to spread information to more than 2VCs.

The only solution I can think of is usage of UserDefaults. I would save an information about a button setting and then call ViewWillAppear in each VC, where the background of the button would be set according to the value in UserDefaults. Is there a better solution, please?

EDIT:

As @cora mentioned in the comments, I was able to solve this using Notification Center.

3
  • Could you add some code? Commented Mar 6, 2021 at 13:50
  • 3
    Using protocol & delegate method is a 1 to 1 relationship to pass data. The 1 to many is to use the Notification Center. Commented Mar 6, 2021 at 13:54
  • I am going to check and try notification center. Thanks Commented Mar 6, 2021 at 14:05

3 Answers 3

2

You have several options, including:

  • Pass an array of the tab controllers to your "popup settings" controller and call a "settingSelected" func in each one directly.
  • Using Protocol / Delegate pattern, you could create an array of delegates in your "popup settings" controller.
  • You can use Notification Center.
  • You could subclass the button and use UIAppearance proxy.

Which approach to use will depend on a number of factors, based on exactly what all you need to do (are there other "settings"? or only that button background?)

You may want to search for swift using themes to see various different approaches.

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

1 Comment

Thank you very much! This is exactly what I needed. I will check all the possibilities.
0

“Is there a better solution, please?”

Not necessarily. Is this in fact a user default, to be preserved between launches? Then this is exactly what user defaults are for.

If not, then at least you need some central location where information about the current button color can be stored. An obvious candidate here is the tab bar controller itself. It gets notified every time there is a tab bar item switch, so it’s a perfect candidate.

Comments

-1

Since you have mentioned you are using a UITabbarController, you can use an instance of UITabbarController and then access .viewControllers property of it and call their added public methods to get them triggered on events that you add. Additionally, using Notification Center makes more sense to me since your code will be more readable. Sometimes Notifications can just get a little confusing for new developers who are working on your code.

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.