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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:02:00+00:00 2026-06-01T08:02:00+00:00

I’m really struggling with with, and I know how easy it is but I’m

  • 0

I’m really struggling with with, and I know how easy it is but I’m just not getting it to work.

I’m very new to Obj-C…

Basically, this app is very simple.

It piles on modal view controllers one on top of the other until the end where the user gets a result and then they are all dismissed and the user can start again.

In each of the modal view controllers, I want to, for example, add 1 to an int I have declared in the first screen.

This is the declaration in the first view controller.h

int total;

This is what I am doing in the 2nd view controller.m

FirstViewController *fvc = [[FirstViewController alloc] init];
[fvc setTotal:1]; //or the following, i can't see what the difference is
fvc.total = 1;

And this seems to do the trick, it sets the variable that I’ve declared in another view.

However, in the 3rd view controller.m, I do this:

FirstViewController *fvc = [[FirstViewController alloc] init];
NSLog(@"Current total: %x", fvc.total);

This works, as in I don’t get errors, but the integer has reset to 0. Ideally, it would say ‘1’, to show it’s retained the value…

My question is; how can I reuse and add to this integer throughout my entire app?

Please also make answers relevant to strings, as I would like to do that also.

Sorry, I’m still learning.

Thanks in advance.

  • 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-01T08:02:02+00:00Added an answer on June 1, 2026 at 8:02 am

    If you define a variable as static, it retains its value across different instances. You could do this in your class like this:

    @interface FirstViewController : UIViewController
    
    - (NSInteger)sharedValue;
    - (void)setSharedValue:(NSInteger)value;
    
    @end
    
    
    @implementation FirstViewController
    
    static NSInteger sharedValue = 0;
    
    - (NSInteger)sharedValue
    {
        return sharedValue;
    }
    
    - (void)setSharedValue:(NSInteger)value
    {
        sharedValue = value;
    }
    
    @end
    

    Now any instances of FirstViewController that you create will share the same copy of sharedValue, and if you set it on one, and get it from another it will work the way you want. The same process works with strings, etc, but if you aren’t using ARC, be careful about retaining and releasing the sharedValue.

    A better approach than creating multiple static values on your class is to create a single shared class on which you can set and get multiple properties. These shared classes are sometimes called “Singletons” because they are classes that have a single instance.

    You can see lots of places where Apple uses singletons, for example [UIApplication sharedApplication], [NSNotificationCenter defaultCenter], [NSUserDefaults standardUserDefaults] – these are all examples of classes where there is a single instance re-used throughout the app.

    To create a singleton, create a new class of type NSObject, like this. You can then add any properties you want to it and access them anywhere in the app via the sharedInstance:

    @interface MyObject : NSObject
    
    @property (nonatomic, assign) NSInteger someInteger;
    @property (nonatomic, strong) NSString *someString;
    
    + (MyObject *)sharedInstance;
    
    @end
    
    
    @implementation MyObject
    
    @synthesize someInteger, someString;
    
    + (MyObject *)sharedInstance
    {
        static MyObject *sharedInstance = nil;
        if (sharedInstance == nil)
        {
            sharedInstance = [[self alloc] init];
        }
        return sharedInstance;
    }
    
    @end
    

    You can now access the shared properties anywhere in your app by saying

    [MyObject sharedInstance].someInteger = 5;
    
    ...
    
    NSInteger value = [MyObject sharedInstance].someInteger;
    

    This is definitely the best way to store global data in your app.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.