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 6325629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:55:50+00:00 2026-05-24T16:55:50+00:00

I have been trying for days to get this code to work, but I

  • 0

I have been trying for days to get this code to work, but I have no idea what I am doing wrong. Everytime the app wakes up from sleep, or the user closes the app and opens it again (without closing the app from multitasking), I want a label value to change.
In my applicationDidBecomeActive, I am running a counter, which I want to display on whatever viewcontroller is open at that moment.

Code:

- (void)applicationDidBecomeActive:(UIApplication *)application {
counter = counter + 1;

W1G1 *view1 = [[[W1G1 alloc] initWithNibName:@"W1G1" bundle:nil] retain];
[view1 setlabel];

}

In my viewcontroller W1G1, I have the following code:
Code:

- (void) setlabel {
NSString *string = [NSString stringWithFormat:@"%d", counter];  
vocabword.text = string;

}

I have imported W1G1 in my appdelegate, but the code does not run 🙁 Please help!

Thanks

  • 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-24T16:55:51+00:00Added an answer on May 24, 2026 at 4:55 pm

    1) When you say ‘the code does not run’ do you mean that? That is, if you put NSLogs in applicationDidBecomeActive: and in setLabel does it show the code is run?

    2) I would suspect the code is running. But your code won’t “show the counter on whatever view controller is open at that moment”. Your code creates a new view (view1), but that view won’t be displayed. It is not added as a subview to anything. Your code will also leak. You create a W1G1 object, but it is never released and you throw away any reference you have to it.

    To achieve what you want, you could add a subview to the application’s window. Depending how your app delegate is set up, something like the following should do the trick:

      counter++;
      W1G1 *viewController1 = [[W1G1 alloc] initWithNibName:@"W1G1" bundle:nil];
      [viewController1 setlabel: counter];
      [[self window] addSubview: [viewController1 view]]
      // you'll want to save a reference to the viewController somehow so you can release it at a later date
    

    Then in W1G1

    - (void) setlabel: (int) counter;
    {
        NSString *string = [NSString stringWithFormat:@"%d", counter];  
        vocabword.text = string;
    }
    

    There are, of course, lots of other approaches you could take towards this problem. And you’ll need some strategy for removing the W1G1 view that you are adding at some stage, otherwise you’ll just get more and more views added.

    Update: You ask (in comments) how to keep track of your viewController throughout lifetime of the app… One approach is to keep track of it in your appDelegate. In the header have something like:

    @class W1G1;
    @interface MyAppDelegate : : NSObject <UIApplicationDelegate>
    
    {
        // other decelerations
        int counter;
        W1G1 * _myW1G1
    }
    
    @property (nonatomic, retain) W1G1* theW1G1
    

    In the .m file include

    @synthesize theW1G1 = _myW1G1;
    

    Probably in application:didFinishLaunchingWithOptions: create the viewController, set the property to refer to it, and add its view to the view hierarchy.

     W1G1* theViewController = [[W1G! alloc] initWithNibName: @"W1G1" bundle: nil];
     [[self window] addSubview: [theViewController view]];
     [self setTheW1G1: theViewController];
     [theViewController release];
    

    Then when you want to access the viewController again from with the app delegate use [self theW1G1], e.g.

    [[self W1G1] setlabel: counter];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get this to work for about three solid days
I've been trying to get this to work for the last two days but
I've been trying to get this to work for days upon days and it
I have been trying to get around this error for a day now and
This has been driving me crazy for 2 days. I have been trying to
For a few days now I have been trying to get an ASMX webservice
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to work my way through Project Euler, and have noticed
I have been trying to make a case for using Python at my work.
I have been trying to learn how to add testing to existing code --

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.