PappaSmalls
PappaSmalls

Reputation: 349

View properties resetting after view change

I've been trying to make this work for a while now with no chance, every time I change from view to view, the view's variables and content are reset to 'default' e.g if in a view I changed a label's text field from the default 'Label' to 'Hello, and then change views, once I come back to the same view, the text will be 'Label' again.

The only way I've gotten around this so far is to set a static string and then change Label.text to string in viewDidLoad. I just KNOW that this isn't the way to do it. I have a hunch that it's to do with how I transition from view to view, (allocating and initiating etc.)

Current way I transition:

FirstView.h:
@interface FirstView : Engine
@property(nonatomic, readwrite) MainGameDisplay *secondView;

FirstView.m:
@implementation FirstView

- (IBAction)StartGame:(id)sender 
{ 
    if (! self.secondView)
    self.secondView = [[MainGameDisplay alloc] initWithNibName:nil bundle:nil];
    [self presentViewController: self.secondView animated:YES completion:NULL];
}

And MainGameDisplay:

MainGameDisplay.h:
@class ViewController;

@interface MainGameDisplay : Engine
@property (strong) ViewController *firstPage;

MainGameDisplay.m:
@implementation MainGameDisplay

- (IBAction)returnToHome:(id)sender {
    if (!self.firstPage)
        self.firstPage = [[ViewController alloc] initWithNibName:nil bundle:nil];
        [self presentViewController: self.firstPage animated:YES completion:NULL];
}

What I would like is to not have to set all of the values again through viewDidLoad, I just cant see it as being a good programming style.

Upvotes: 0

Views: 693

Answers (1)

Phillip Mills
Phillip Mills

Reputation: 31026

You're right in your suspicions about what's gone wrong. Although you call your method returnToHome: you aren't really returning anywhere but rather stacking a new copy of ViewController on top of whatever you already have.

To actually go back, the opposite of presentViewController: is dismissViewControllerAnimated:. Try using that inside your MainGameDisplay class when the button is pressed to go back.

Upvotes: 1

Related Questions