What is the preferred method of accessing ivars from different classes?
Application Delegate Class
Say I want to access the root controller (@synthesized as rootController) from the Application Delegate class in another UIViewController class. I’ve read somewhere that you access ivars from the Application Delegate class differently than you access other ivars.
Regular Class
If I want to access some ivars from lets say another UIViewController class. I would like to access the ivar which contains a list (NSArray) of names (@synthesized as names) in class A and get access to them from class B.
The ivars have default access (protected).
Perhaps you can point me to a good tutorial explaining how to access ivars.
The application delegate is a singleton so you can access those properties from anywhere.
In the case of a ‘normal’ class, and assuming you don’t want to make it a singleton, you would normally use the delegate pattern. This means that class A becomes the delegate for class B and class B can call methods that class A will implement, this is how UITableViews work with the DataSource delegate.
This only works if you only need to access these properties from one other class (delegates don’t support multi-delegates without a bit of hackery), otherwise I would encapsulate your data in a model which any class can access.