
For the header that covers it. The rest of Singleton class work is in the implementation. Have a look at listing 2 below. As usually create the getter and setter property using the @synthesize directive. Next is the body of the singleObj method. This is where the Singleton actually takes shape. Create a static singleObj variable and set it to nil which always be an instance of itself.
Next use the @synchronized which will synchronize the different threads of the Singleton object so that our object variable, glbStr will always that an instance of itself and return the desired information no matter where you are. Next we check to see if the Singleton object already exists, if it doesn’t create it otherwise return the existing one. That is it for the Singleton.
It is a very simple design pattern. Next I will demonstrate how to implement it to pass data between two view controllers. For this example, you will need two view controllers at least, more is ok too. Lay out the view controllers as in Figure 1 and connect them both with a Tab Bar Navigator. Next open the header file of the first view controller, klViewController, and create a Singleton variable called, sobj. We will initialize this variable in the implementation viewDidLoad method.
The two other properties are created by adding IBOutlets through the storyboard. One of the IBOutlet is a UILabel to display the value of the sobj, the other is the UITextField.The same goes for the IBAction. See listing 3 below. In the implementation file, there is no need to synthesize the Singleton variable because it’s not a property with a getter/setter.
Initialize the Singleton variable in the viewDidLoad method as in listing 4. This method gets called when the app is first loaded. Every time you transition from one view to another this method never will get called except on first load. Subsequent processing of the view controller will need to be done in the viewWillAppear or the viewWillDisappear methods.
We will these methods to access the sobj variable and its value as we transition from one view controller to the next and back. In the addToSingleton method, listing 5, I assign the value of the UITextField, enterValue to the sobj variable. Since we are using the @synchronize directive, this value will be carried forward to other objects or until it is replaced by another value. The resignFirstRespsonder is there to dismiss the keyboard.
To test the code add another view controller to the canvas and connect it to the Tab Bar Navigator controller. Add a UITextField to enter data and an UILabel to display data from the Singleton variable. Once the UI is laid out, make your IBOutlets and connect them. You are also going to need a second custom UIViewController for your view controller in the storyboard.
Create a new custom UIViewController class as a subclass of a UIViewController class. Once the class is create, open the storyboard and select the second view controller. Activate the Identity inspector and add the custom UIViewController class to the view controller. In the implementation file, the three main methods are viewdidLoad, viewWillAppear and viewWillDisappear.
The viewDidLoad method will set the Singleton variable to itself so we will have access to the Singleton from the first view controller, listing 7. This method is only called the first time the view is loaded. Immediately after the viewDidLoad method is called, the viewWillAppear method is called just before the view controller is displayed on screen, listing 8. This method will assign the value of the gblStr to the UILabel: displayValues.
0 Comments