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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:56:21+00:00 2026-05-30T04:56:21+00:00

This is probably a silly question but I’m having a hard time trying to

  • 0

This is probably a silly question but I’m having a hard time trying to solve it. The thing is that a have a class that implements a counter and at the end of it procedures it calls a view controlled by another class. What I’d like to do is access the value of the counter of the first class from the second one. I defined the counter as a property and tried to access it from the other class but I always get its value as 0. Can anyone help me out?

thanks.

What i have is this:

Class1.h

@interface Class1 : CCLayerColor <UIAlertViewDelegate>
{
    int movesCounter;
}
@property int movesCounter;

@end

Class1.m

@implementation Class1
@synthesize movesCounter;

//At this point the counter gets incremented and NSLogging its value correctly

@end

Class2.m

#import Class1.h    

@implementation GameOverLayer


-(id) init
{
    if( (self=[super init]))
    {
       Class1 *theClass1 = [[Class1 alloc]init];
       NSString *message = [NSString stringWithFormat:@"It took you %d moves.",theClass1.movesCounter];
       UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

       [alert show];
       [alert release];
    }
}

What am I doing wrong?

  • 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-30T04:56:22+00:00Added an answer on May 30, 2026 at 4:56 am

    Your problem is that you’re creating a new instance of Class1 in your Class2 init method:

    Class1 *theClass1 = [[Class1 alloc] init];
    

    This new instance is in addition to the one with the int you’ve been incrementing successfully, and as it’s a fresh slice of memory, its value for movesCounter is still 0. What you really want is a pointer to the specific copy of Class1 that you’ve been working with — no alloc / init needed or desired. Try making a property in Class2 that can hold your pointer to Class1:

    #import "Class1.h"
    
    @interface Class2 : NSObject {
        Class1 *myPointerToClass1;
    }
    @property (nonatomic, retain) Class1 *myPointerToClass1;
    

    And of course @synthesize myPointerToClass1 in the .m of Class2. Now that Class2 is ready to hold a pointer to Class1, you need simply to populate it. In Class1, after the code that alloc’s and init’s the Class2 instance, use code like this:

    myClass2.myPointerToClass1 = self;
    

    Just to make sure I haven’t forgotten anything I did all this using your code above and everything worked just fine for me.


    EDIT: original answer begins here, but is now irrelevant…..
    You mention having defined the counter as a property, but have you also synthesized the instance variable in your implementation (.m) file?

    @synthesize myCounter;
    

    This will create the setter and getter methods automatically for you. Also, have you verified that the value is being incremented at all, perhaps with an NSLog statement in the viewcontroller?

    NSLog(@"myCounter is currently equal to %d", myCounter); // assuming an int
    

    If these basics are covered, maybe you could edit your question to include the code showing us more about how the misbehaving class is getting this pointer to the variable in the first place.

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

Sidebar

Related Questions

This is probably a silly question, but I am having a hard time finding
Probably a silly question here but I have been trying to find this for
This is probably a silly question, but here it goes.Imagine you have the following
This is probably a silly question, but I am trying to stuff an anonymous
This is probably a silly question but I have been thinking it over for
This is probably a silly question, but i could't figure it out. I have
This probably might be a silly question, but what I am trying to do
This is probably a silly question, but curiosity has gotten the better of me.
This is probably a very silly question. I am trying to do a geolocation
This is probably silly but I don't have enough Elisp knowledge to understand what

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.