Passing data between segues without using PrepareForSegue

江枫思渺然 提交于 2019-12-06 02:54:44

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.

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.

Try it.

let yourInstance = NSUserDefaults.standardUserDefaults().objectForKey("yourKey")
NSUserDefaults.standardUserDefaults().synchronize()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!