ios - How do I create a single instance of a class and update the data for that one class in many views? -



ios - How do I create a single instance of a class and update the data for that one class in many views? -

i'm creating ipad application i'll utilize conduct surveys among employees. application contain multiple uiviewcontrollers.

what i've done far:

i created "survey" class. class contains properties fields i'm going filling in survey progresses. basic things name, email address, age, etc.

i created subclass uiviewcontroller called svviewcontroller. in class, created , initialized instance of survey called survey after importing survey class.

i went each of uiviewcontroller classes created , associated storyboard view , changed inheritance inherit svviewcontroller.

i'm still new this, , i'm having problem figuring out how i'm going proceed. want instance of survey (again, called "survey") contain info user fills in. when enters name in first view, want communicate instance of survey update name. want go next view , pull value of "name" instance of survey.

basically, need survey, 1 instance of survey, persist throughout views. found illustration online regarding singleton pattern, , started that, i'm having trouble. before go further, paste code:

survey.h #import <foundation/foundation.h> @interface survey : nsobject @property (nonatomic, strong) nsmutablestring *name; @property (nonatomic, strong) nsmutablestring *emailaddress; @end survey.m #import "survey.h" @implementation survey @end svviewcontroller.h #import <uikit/uikit.h> #import "survey.h" @interface svviewcontroller : uiviewcontroller { survey *survey; } @property (nonatomic, retain) survey *survey; +(id)sharedmanager; @end svviewcontroller.m #import "svviewcontroller.h" @interface svviewcontroller () @end @implementation svviewcontroller @synthesize survey; +(id)sharedmanager{ static svviewcontroller *sharedsvviewcontroller = nil; @synchronized(self){ if(sharedsvviewcontroller == nil) sharedsvviewcontroller = [[self alloc]init]; } homecoming sharedsvviewcontroller; } - (id) init{ if(self = [super init]){ survey = [[survey alloc] init]; } homecoming self; } - (void)viewdidload { [super viewdidload]; // additional setup after loading view. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end

i'm sure kind of mess, , tried toying around long time. think what's happening single instance of survey beingness created, when seek things setting values properties of survey (name, emailaddress, etc) , rewrite them check see if changed, i'm getting nulls back. i'm assuming because i'm not initializing property anywhere, i'm having lot of problem figuring out it.

thank in advance.

you want create single instance of survey, looks have written code create single instance of svviewcontroller. instead, seek this:

@interface survey : nsobject + (instancetype)sharedinstance; @property (nonatomic, strong) nsmutablestring *name; @property (nonatomic, strong) nsmutablestring *emailaddress; @end @implementation survey + (instancetype)sharedinstance { static survey *_instance; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ _instance = [[survey alloc] init]; }); homecoming _instance; } @end

ios objective-c iphone uiviewcontroller

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -