Objective-C Singleton Objects and Global Variables - iphone

I'm aware of other posts on this topic but I'm only really one rung up the ladder from being a noob so need a bit more help.
My iPhone app has several global variables - some I have declared and given values in a class but others need to be set during a login process (like a token for example) that then need to be accessible for the lifecycle of the app from any class or method. I am told I should really be using a Singleton object for all of this which I presume is a class that's instantiated on startup. If so, could someone give me the simplest example of such header and implementation file and how/where I should instantiate it? Then I need to have some strings that are set from the off and others that can be set/got later on?
Thanks very much in advance. Also, I'm new here so if my etiquette is off in any way, please let me know.
Thanks,

This link shows some code to create a singleton class : http://www.galloway.me.uk/tutorials/singleton-classes/
You would use it something like :
[[MyManager sharedManager] doSomething];
The call to sharedManager would get the one instance of the class (or, if this is the first time you called it, would create it) - this makes sure that you only have one of them :)
It also overrides release, retain, autorelease etc to make sure that you can't accidentally get rid of the sharedManager by mistake!
This class will instantiate itself the first time you use it but if you need it to be created on startup, just call [MyManager sharedManager] and it will create it for you.
You define the class like any other objective-c class - just add properties etc
Hope that helps :)

Global variables aren't good, but singletons aren't much better when they're just used to provide global access to some data. Anything bad you can say about a global variable, you can also say about a singleton that's used for global access. A better solution is to create a data model and pass that model from one view controller to the next.
Here's a previous SO question that might help.

Related

Basics Method Override

First of all, Sorry. I'm not good at English.
Hi. I'm beginner of iOS.
To learn Objective-c for the first time
Define the class name and method name in the header file and declare the class object created above in the main.m file on implementation details. M calling the method does.
[NewPoint SetPoint: 3];
[NewPoint print];
But iOS studied, I suddenly was wondering iOS studied.
Without calling the method directly from the main.m, appdelegate.m method override you if you do not like this [NewPoint SetPoint:3] that is the exact reason why I wonder.
Why Running just override method not using [NewPoint SetPoint:3] like this.
For learning purpose it was taught to create class in main.m itself.
But in real programming world, you follow classes which are a blue print, so these are created separately for full use throughout the application and other classes/objects are able to use them.
Classes make your code separate from different classes. AppDelegate is one such class which comes initially with a project.
Later on you will make your own classes like Person, Employee, Box etc each one having their own properties, behaviours etc.
What I feel is in your training you were taught to create a class and object as NewPoint, later it was created in AppDelegate. Now you can try to use a differenct class called NewPoint and make an object of that inside AppDelegate and use it.

Using Objective-C category to avoid compilation error

I did something a little bit nasty today.
All of our view controllers inherit from two different parent view controllers, let's say XXXViewController and YYYViewController. XXXViewController in turn inherits from TrackedUIViewController, which is a class provided in the Google Analytics SDK so all your view controllers can inherit from it and easily track them.
YYYViewController however, inherits from a different type of view controller. Ah, and it's an open sourced piece of code that I really don't want to change.
What's the problem here? We cannot track any of the YYYViewController children because we can't access the methods provided in TrackedViewController, since they are private.
I don't want to modify the source provided in the Google Analytics SDK. So what did I do? Create a category that exposes those methods, just to avoid the compilation error.
The obvious downside to this is that the GA source code changes it may break, but it will be fairly easy to detect.
I was wondering what other problems I could be facing by doing this, and if you guys can think of a better approach.
Thank you
You can go up in the inheritance chain of YYYViewController, see in turn what class it inherits from. If it's UIViewController, simply change that particular superclass to TrackedUIViewController in the source and you're fine to go.
Example for better understanding: suppose YYYViewController inherits from ZZZViewController, which in turn inherits from UIViewController. Now you can change the superclass of ZZZViewController from UIViewController to TrackedUIViewController - since TrackedUIViewController inherits from UIViewController, no functionality will be lost, but magically your whole YYYViewController class will become trackable.
Hope this helps :-)
You already mentioned the biggest risk of using undocumented APIs: Changes to the API are beyond your control and may break your logic, for example if methods are removed/renamed, or their behavior stops matching your expectations.
From the purely technical point of view, I can see no further problems, since in Objective-C all methods are public. As long as they continue to exist, you may continue to call them.
I would say the root problem is excessive subclassing. Keep view controller hierarchies shallow. Use composition instead of subclassing. If you must subclass ensure that functionality that a class provides can be turned on & off by it's subclasses.

Why we use app delegate in our application

I am new in iphone development and i needed a array which i use to access in different class so my senior told me that you should declare in App delegate and access it another class in which it require, so i wrote line for that
MyAppAppDelegate * appObject =
(MyAppAppDelegate*)[[UIApplication sharedApplication]delegate];
I done my task successfully but i couldn't get it 100%. Someone tell that why we use this and in what exactly situation in which we've to use this?
AppDelegate is loaded first when you run your application as it contains window. So, the variable and objects you want to access throughout your project is declared in AppDelegate. You just have to create an instance and you can access all the objects in AppDelegate.
ApplicationDelegate can be useful as a singleton class but you have to use it with discretion - and there are varying opinions on this - but if you have a few global type properties or methods you want to recall from various other classes, and I emphasize few, then ApplicationDelegate may be a nice place to add these.
And yes, it is bad design - but you can get away with it if you are prudent and as #Sedate Alien mentions, take a look at dependency injection.
The purpose of ApplicationDelegate, by the way, is mainly to handle events like loading your application, when you return to home screen, when you come back from home screen, etc.

Trying to message a button from another class

I'm new to Objective-C so I may be doing this completely wrong, and if I am please correct me. I am trying to make a separate class in my iPhone app just for skinning buttons. My hope is that this will allow me to reuse as much code as possible but before I spend too much time on it, I would like to know if its possible/a good idea to send a message to a UI Control from another class, and if i can, how should I do it? right now im trying to pass the sender ID to my SkinTools class and message that but it doesn't look like it will allow me to message the layer object.
So, am I just completely off the wall here, or is this possible?
Consider looking into using the delegate pattern.
One could just use the addTarget:selector: method for this purpose. As target set the class you want to send the message to, as selector the method you want to call on the class.
You could add some iVars to your class, like id buttonTarget and SEL buttonSelector and create an initializer like -initButtonWithTarget:selector: to set these values on initialization.
It turns out categories was the answer I needed, then I can just add a skin method to each control I use. I can even put them all in the same file to make it easy to get to.

Best practice for View-Routing in iPhone SDK

I've run into a little problem while developing a Core Data driven Quiz and be a bit confused about a best practice to solve my problem.
I have approximately five templates for the different questions, which will be loaded in case which question is displayed. So I check which template has question 1 and push the new question-template view into my navigation controller. Because its always the same code I want to write a function (I came from php) which gets the next question-id as argument and decide which template will be loaded and push the next view into the navigation-controller.
What is the best practice to solve this problem? Can I write a function with access to the navigation-controller, and my Core Data classes. And if yes where I have to create this function?
Okay I think I found an way but gets here another Error. I create a class called QuestionRouter and define an class method. I'll import this class into each viewController where it needed. The class method gets the correct template from Core Data without an problem. But now I wan't to load the correct view. For that I need to access the navigationController defined in my AppDelegate.
So how I can access the navigationController in my AppDelegate for another class?
Hope for an answer.
Mister-D