Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Envulos

macrumors newbie
Original poster
Sep 26, 2015
2
0
Hey, I am pure noob and I wanted to ask, how to properly save a variable, that if I change ViewControlers, the variable will be automatically saved and loaded (without a load button). I have heard about a method Singleton, but as I said, I am soooo noob and I don't really understand, how to use it... Thanks for help. (I can paste the code here if it will be easier to solve, just tell me)
 

Dookieman

macrumors 6502
Oct 12, 2009
390
67
Can't really "Save" a variable except if you wanna keep it in a database. If you want to pass a variable to the next viewController. Create a variable that you want to pass it to in the viewController you want to push to. Set the variable with the data you have after you instantiate the controller you are pushing to.

e.g.

Code:
ViewController *viewController = [self.storyboard instantiationViewWithStoryboardID:@"Detail Controller"];

viewController.varibleYouWantToSet = @"APIKEY";

[self.navigationController pushToViewController:viewController animated:YES];
 
  • Like
Reactions: Envulos

Envulos

macrumors newbie
Original poster
Sep 26, 2015
2
0
Do you know how does the Segue method works? Thanks for help :)

Code:
//
//  ViewController.h
//  Clicker
//
//  Created by Tomáš Boček on 26.09.15.
//  Copyright (c) 2015 StudioEnvulos. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    IBOutlet UILabel *Label;

    int  Number;
}

-(IBAction)Addition:(id)sender;
-(IBAction)Save;
-(IBAction)Load;

@end


    //
//  ViewController.m
//  Clicker
//
//  Created by Tomáš Boček on 26.09.15.
//  Copyright (c) 2015 StudioEnvulos. All rights reserved.
//

#import "ViewController.h"
#import "UpgradesViewController.h"
#import "StatsViewController.h"

@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    Label.text = [NSString stringWithFormat:@"%d", Number];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"Upgrades"]){
        // What to write here???
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)Addition:(id)sender{
    Number += 1;
    Label.text = [NSString stringWithFormat:@"%d", Number];
}
-(IBAction)Save {
    [[NSUserDefaults standardUserDefaults] setInteger:Number forKey:@"Number"];
}

-(IBAction)Load {
    Number = [[NSUserDefaults standardUserDefaults] integerForKey:@"Number"];
    Label.text = [NSString stringWithFormat:@"%d", Number];

}

@end


    //
//  UpgradesViewController.h
//  Clicker
//
//  Created by Tomáš Boček on 27.09.15.
//  Copyright (c) 2015 StudioEnvulos. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UpgradesViewController : UIViewController

@end


    //
//  UpgradesViewController.m
//  Clicker
//
//  Created by Tomáš Boček on 27.09.15.
//  Copyright (c) 2015 StudioEnvulos. All rights reserved.
//

#import "UpgradesViewController.h"

@interface UpgradesViewController ()

@end


@implementation UpgradesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
 

Dookieman

macrumors 6502
Oct 12, 2009
390
67
Do you know how does the Segue method works? Thanks for help :)

Code:
//
//  ViewController.h
//  Clicker
//
//  Created by Tomáš Boček on 26.09.15.
//  Copyright (c) 2015 StudioEnvulos. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    IBOutlet UILabel *Label;

    int  Number;
}

-(IBAction)Addition:(id)sender;
-(IBAction)Save;
-(IBAction)Load;

@end


    //
//  ViewController.m
//  Clicker
//
//  Created by Tomáš Boček on 26.09.15.
//  Copyright (c) 2015 StudioEnvulos. All rights reserved.
//

#import "ViewController.h"
#import "UpgradesViewController.h"
#import "StatsViewController.h"

@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    Label.text = [NSString stringWithFormat:@"%d", Number];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"Upgrades"]){
        // What to write here???
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)Addition:(id)sender{
    Number += 1;
    Label.text = [NSString stringWithFormat:@"%d", Number];
}
-(IBAction)Save {
    [[NSUserDefaults standardUserDefaults] setInteger:Number forKey:@"Number"];
}

-(IBAction)Load {
    Number = [[NSUserDefaults standardUserDefaults] integerForKey:@"Number"];
    Label.text = [NSString stringWithFormat:@"%d", Number];

}

@end


    //
//  UpgradesViewController.h
//  Clicker
//
//  Created by Tomáš Boček on 27.09.15.
//  Copyright (c) 2015 StudioEnvulos. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UpgradesViewController : UIViewController

@end


    //
//  UpgradesViewController.m
//  Clicker
//
//  Created by Tomáš Boček on 27.09.15.
//  Copyright (c) 2015 StudioEnvulos. All rights reserved.
//

#import "UpgradesViewController.h"

@interface UpgradesViewController ()

@end


@implementation UpgradesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

I know how it works, but I don't use it.I prefer the traditional UINavigationController push because to access the variables in another controller requires two delegate methods while the traditional way requires just one.

If you're super green, here is a good turtorial.
http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
 
  • Like
Reactions: Envulos

smirk

macrumors 6502a
Jul 18, 2002
691
54
Orange County, CA
You could do something like this:
Code:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   if ([segue.identifier isEqualToString:@"Upgrades"]) {
      [[segue destinationViewController] setSomeData:self.someData]; // Either put some data in a property on the destination view controller...
      [[segue destinationViewController] setPreviousVC:self]; // Or even pass the whole class to it so it can access any public methods and properties.
   }
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.