3

I'm creating an user setup account with 5 steps using storyboard. Each step have a ViewController: 1º)Input for Name, contact etc, 2º) Import photos, 3º)Input, etc 4º)more inputs 5º)Confirmation Page, if the user click "confirm" -> Get all the inputs and upload to Parse.

The only solution i get when i search online for this, is to create a func "Prepare for Segue" and pass the information...But for me, this doesnt make any sense:

If i had 1000 viewcontrollers, the first viewcontroller information will be passsed through all the 1000 viewcontrollers? Why not the nº1000 view controller getting all the information that was left behind? e.g: The viewcontroller nº50 dont need the information about the viewcontroller nº1... This is a nonsense.

Is there any other way to implement this?

Maybe there is a solution since i'm using Parse like when i do:

ParseClass["Name"] = textfield.text

It sends information to Parse and they hold it until i do something like SaveInBackground().

I'm trying to find a solution like this using viewcontrollers. Each viewcontroller send information to parse and hold the information until the "saveinbackground()" happens on Viewcontroller nº5.

Possible? thank you

rici
  • 234,347
  • 28
  • 237
  • 341
Renato Pimpão
  • 271
  • 3
  • 17

3 Answers3

5

Yes it is possible. You can use NSUserDefaults for that which will store your info into memory and once it is stored you can use it anywhere in your project.

Like you can store value with it this way:

NSUserDefaults.standardUserDefaults().setObject(yourObject, forKey: "yourKey")

Now you can retrive this info from any where with yourKey like this:

let yourInstance = NSUserDefaults.standardUserDefaults().objectForKey("yourKey")

And you can cast it's type as per your need.

For more Info read Apple Document for NSUserDefaults.

One more way to pass value from one view to another view without segue by using Class or Struct.

You can read more about Classes and Structures from Apple Documentation.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • 2
    I would urge against using NSUserDefaults for normal application data and use a model class as @Johnykutty suggests. – Luke Aug 07 '15 at 12:20
1

If our inputs are finite, create a model class with properties for it ex:

@interface UserDataInput : NSObject
@property (nonatomic)NSString *name;
@property (nonatomic)NSString *contactNumber;
....
blah blah bla
....
@end

then make it as a singleton class

@implementation UserDataInput
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static UserDataInput *sharedInstance = nil;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[[self class] alloc] init];
    });
    return sharedInstance;
}
@end

Then set properties from a view controller on leaving that view controller like,

UserDataInput *sharedInput = [UserDataInput sharedInstance];
sharedInput.name = self.nameField.text;
etc....

In final view controller you can access these properties to upload to parse.

Johnykutty
  • 12,091
  • 13
  • 59
  • 100
0

Try it.

let yourInstance = NSUserDefaults.standardUserDefaults().objectForKey("yourKey")
NSUserDefaults.standardUserDefaults().synchronize()
Sankalap Yaduraj Singh
  • 1,167
  • 2
  • 14
  • 32