Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data between segues without using PrepareForSegue

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

like image 889
Renato Pimpão Avatar asked Mar 17 '26 03:03

Renato Pimpão


2 Answers

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.

like image 198
Dharmesh Kheni Avatar answered Mar 19 '26 16:03

Dharmesh Kheni


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.

like image 38
Johnykutty Avatar answered Mar 19 '26 17:03

Johnykutty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!