Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:17:32+00:00 2026-05-31T13:17:32+00:00

Usually I just put together an app in any random way, as long as

  • 0

Usually I just put together an app in any random way, as long as it works, but this means I don’t pay any attention to any design patterns. My application currently makes extensive use of global variables (I carry an instance of my AppDelegate in every view controller to access properties I declared in my AppDelegate.h). Although it does what I want it to do, I’ve read this is not a good design practice.

So I want to start making my code “legit”. However, I can’t imagine my app right now without global variables. They are so important to the well-being of the app, but this must mean I’m doing something wrong, right? I just can’t imagine how else I’d do some things. Take for example this:

enter image description here

You have two view controllers here, a SideViewController and a MainViewController. Using global variables, such as say if the entire application had one shared instance of SideViewController and MainViewController (appDelegate.sideViewController and appDelegate.mainViewController), I can easily communicate between the two view controllers, so that if I press “News Feed” in my SideViewController, I can tell my MainViewController to reload it’s view.

I can’t imagine, however, how this would be done if these were not global variables? If an event occurs in my SideViewController, how would I notify my MainViewController, in a way that is in accordance with design standards?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:17:33+00:00Added an answer on May 31, 2026 at 1:17 pm

    I can’t imagine, however, how this would be done if these were not
    global variables? If an event occurs in my SideViewController, how
    would I notify my MainViewController, in a way that is in accordance
    with design standards?

    The same way you do it now, except that the SideViewController gets its reference to the MainViewController from a different place.

    How are these two view controllers created? It’s likely that it happens in one of two ways:

    1. One of the objects creates the other. In this case, maybe the MainViewController creates the SideViewController.

    2. Some other object, such as the app delegate or another view controller, creates them both.

    In the first case, the MainViewController has a reference to the SideViewController as soon as it creates it. It can store that reference in one of its own instance variables, so that it can always send messages to the SideViewController that it created. Similarly, the MainViewController can give the SideViewController a reference to itself (that is, to the MainViewController), and the SideViewController can store that and use it in the future to talk to its MainViewController.

    The second case is similar — if the app delegate (or some other object) creates both MainViewController and SideViewController, that object knows about both objects and can configure each with a reference to the other.

    In both cases, the objects in question are able to communicate with each other just as easily as they ever did and there’s no need for a global variable.

    What I’ve explained above is perhaps the simplest way to accomplish what you asked for — communication between two objects. There are a number of patterns that can be used to refine the relationship between those objects to make your code even better:

    • delegation: Give SideViewController a delegate property, and define some protocol that establishes what SideViewController expects of its delegate. Implement that protocol in MainViewController. Make your MainViewController instance the SideViewController’s delegate. SideViewController doesn’t need to know exactly what type its delegate is — it only cares that it’s delegate implements the required protocol. This makes it easy to use SideViewController with something other than MainViewController if that opportunity arises, or to use it in a different project.

    • notifications: SideViewController may not even need a delegate — it can simply broadcast notifications about certain events to any object that happens to be listening. This is particularly effective if more than one object might need to know about something that happens in SideViewController, or if the objects that care about SideViewController’s actions might change.

    • MVC: Instead of telling MainViewController that something has changed, SideViewController just changes the data in the model. Whenever the MainViewController’s view appears (or any other view controller’s view, for that matter), the controller reads the data from the model and redisplays itself.

    If you’re interested, you might want to pick up a copy of Erik Buck’s Cocoa Design Patterns, which explains these patterns and many others in great detail. Don’t feel like you have to learn it all at once, or that it’s all too much trouble. Learn a little bit at a time and see how it improves (or doesn’t) your projects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually put my mod_rewrite conditions in an .htaccess file, but this is a
I know that this is usually not the way to use AngularJS but I
When I first started with Javascript, I usually just put whatever I needed into
I am currently deploying a Rails application using Capistrano. Cap deploy:update_code usually works just
I'm trying to put some simple jQuery code into the Wordpress header (usually just
Multi-threading usually means locking critical sections, etc. So I can't help but wonder, in
Is there any way in CSS to put a limit on how far something
Couch has a REST interface. This means that data-updates are exclusive to PUT calls.
I'm stuck on an mysql query. I usually just put the result set into
I just requested a hotfix from support.microsoft.com and put in my email address, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.