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

  • Home
  • SEARCH
  • 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 9135815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:52:20+00:00 2026-06-17T08:52:20+00:00

I have a singleton implemented in my app, but I’ve run into a problem

  • 0

I have a singleton implemented in my app, but I’ve run into a problem when I switch between view controllers.

My app starts in one view controller, MainMenu, then switches to the Game view controller when the menu selection is made. I have a singleton class in the Game VC that is the object manager (called World) for the game. It loads content based on the menu selection in MainMenu. I can load up and quit back to the MainMenu VC fine. Singleton works fine. When I select from the MainMenu again (going from MainMenu VC to Game VC), my app crashes cause of an NSAssert to prevent the singleton World from allocating a second time, which is intentional.

How can I switch back to the Game VC without it trying to reinitialize my singleton?

Essentially, I want to skip the [[world alloc] init] line in my Game VC’s init method. I can’t figure out how to do this properly… it would need to be able to be handled the first time (when World doesn’t exist) or any subsequent time (when World already exists as a singleton). I’ve tried if (!world) and that doesn’t work.

This also begs the question… did I implement my singleton in the right place? Should I perhaps put it in the MainMenu instead? I just want to avoid it trying to reinitialize / reallocate the singleton when I switch between the two VCs.

If this helps, I’m exiting the Game VC by using [self.view removeFromSuperview]; Should I do it a different way?

  • 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-06-17T08:52:20+00:00Added an answer on June 17, 2026 at 8:52 am

    I was able to solve this by indeed moving the singleton initialization up into the MainMenu view controller as I was thinking. In my design, init for that VC is only called once upon app startup, whereas the custom init method in my Game VC gets called each time I switch to it.

    Not sure if that’s the optimal design I should be using, but it works for now.

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

Sidebar

Related Questions

I have implemented iAds into my app successfully in one particular view controller, and
I have successfully implemented iAd into my App just this past week, but when
I have a singleton implemented like this: class Test123(object): _instance = None def __new__(cls,
Looking at this article: http://www.galloway.me.uk/tutorials/singleton-classes/ I have implemented a singleton, my question is the
I have a singleton class which is being used throughout the app. I am
I have implemented the following code but my SearchActivity::OnCreate is not getting called. Can
I have an Android app, in which Activities fire long running operations that run
I have a Logger class which is implemented singleton. The class is simple and
I just recently implemented: https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets I have imported all the necessary files into my
I have searched through SO looking for different ways to share data between view

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.