I have a textViewController class. I want to set(basically update) the corresponding textView’s content from another view controller which I am pushing over the present textViewController. The way I thought I could do this was to have a shared singleton class and have a string property there to contain the text. I have been using the singleton for storing some other content as well and it worked fine till now.
But the text variable in singleton class doesn’t hold the content I pass to it from the second view controller and hence the textViewController, after popping the second view controller, displays the old text even after reappearing. I am updating the textView in its viewWillAppear method which is getting called but shows old text content on NSLogging.
What am I missing here? Please suggest a solution, stuck at it for a long time now.
Declaration
Firstly, declare the
NSStringin your app delegate.hfile. It should look something like this:Now you want to synthesize the object so that the accessor and mutator methods are made for you. This means you don’t have to write the
setSharedString:andgetSharedStringmethods – we can simply access and mutate the object by making a reference to it. This is how we synthesize it in the.mfile:Assigning a Value
When you want to assign a value to
sharedStringfrom another class, you must first retrieve the active instance of your application delegate:The
appDelegateobject here is your running app delegate instance. To access thesharedStringNSStringobject in the delegate and assign a value, we’d do this:For the duration of our application’s runtime, “some string we want to assign” is now stored in
sharedStringin our app delegate.Retrieving the Value
You’ll want to retrieve the value at some point. To do this, again we’ll have to get the running instance of our application delegate:
Once we have the running instance, we can then access the
sharedStringobject stored inside it:From this point,
retrievedStringnow holds the value “some string we want to assign“.Note: