简体   繁体   中英

How can I make my app remember an integer?

In one of my ViewControllers, I set an integer and what it equals. I am trying to put this integer into the NSLog in a later ViewController to see if it remembers it, and it thinks it is 0, which it is not. I am pretty new to programming, so I am really confused. I did not release the integer, and I thought maybe that would do it. What do I do?!

UPDATE:

The int is in StarsViewController.h, StarsViewController.m, and StepOne.m.

StarsViewController.h

#import <UIKit/UIKit.h>

@interface StarsViewController : UIViewController {
int typeofstar;
}

@property (nonatomic) int typeofstar;
@end

StarsViewController.m

#import "StarsViewController.h"
#import "StepOne.h"

@implementation StarsViewController
@synthesize typeofstar;


#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
typeofstar = 1;
NSLog(@"%i", typeofstar);
}


- (IBAction)proceed {
StepOne *one = [[[StepOne alloc] initWithNibName:@"StepOne" bundle:nil] autorelease];
// you can change flip horizontal to many different other transition styles.
one.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:one animated:YES];
}

@end

StepOne.m

// I only included the important part:
- (void)viewDidLoad
{
    [super viewDidLoad];
    StarsViewController *type = [[StarsViewController alloc] autorelease];
    NSLog(@"%i", type.typeofstar);
}
StarsViewController *type = [[StarsViewController alloc] autorelease];

This line (above) creates an instance of StarsViewController. This is not the same instance (the same object) as the other StarsViewController you just came from.

So this new instance of StarsViewController has its own 'typeofstar', which will initially be zero.

Does that make sense?

EDIT:

How to fix this:

Well, you could pass things along directly from one view controller to another. You could make a property on the StepOne view controller that you could set just before you present it. You're doing that now in fact if you look at how you are setting the modalTransitionStyle on your StepOne view controller. That's a property. You could make another property called "typeOfStar" and set it the same way.

You have many other options for sharing data as well. When your application is running you have to think of it as lots of objects that are in memory at any given time. Your application delegate is one object that is pretty easy to get to from anywhere, so people do use that to stash small things they want to use throughout the app.

You can look at global variables as another option. (use wisely!)

As your needs get more complex, you might have other objects that you keep around as singletons or hanging off of singletons.

Hope that helps.

Perhaps the problem is that you never initialize the StarsViewController instance. Try something along the lines of the following:

StarsViewController *type = [[[StarsViewController alloc] initWithNibName: @"StarsView" bundle: nil] autorelease];

Also, in reference to Firoze's answer, each stars view controller will have its own type ivar. If you want to have a global type, look into static variables or NSUserDefaults .

Going for a singleton or a Data class , I think will be the best solution for you. You can declare a variable in singleton class and access it anywhere in application. This way the value of variable (integer in your case) is preserved.
This is how singleton/Data class works:

//DataClass.h

@interface DataClass : NSObject {    

int i;   

}    
@property(nonatomic,assign)int i;    
+(DataClass*)getInstance;    
@end  

//DataClass.m

@implementation DataClass    
@synthesize i;    
static DataClass *instance =nil;    
+(DataClass *)getInstance    
{    
    @synchronized(self)    
    {    
        if(instance==nil)    
    {    

        instance= [DataClass new];    
    }    
}    
return instance;    
}    

Now in your view controller you need to call this method as:

DataClass *obj=[DataClass getInstance];  
obj.i= // whatever you want;  

This variable will be accessible to every view controller. You just have to create an instance of Data class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM