Peter Warbo
Peter Warbo

Reputation: 11728

iOS Objective-C accessing ivars from different classes

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.

Upvotes: 1

Views: 1056

Answers (3)

Simon Lee
Simon Lee

Reputation: 22334

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.

Upvotes: 1

sergio
sergio

Reputation: 69047

If I understand correctly your question, you will want to get a solid grasp at the Model-View-Controller pattern (which, by the way, is ubiquitous in iOS).

Basically, you should distinguish between your Views and your Controllers -- this is very "natural" in iOS because it is enforced by the framework -- but also between controllers/views and your Model, which is where you have your data, in your case the list of names. Controllers access the model and modify it and also mediate access to it from the Views.

The Model should be factored out someway -- say, in a class of its own -- in order to allow for modularity and low dependencies. In one particular desing (not meaning it is the best or more appropriate for you) the Model could be a Singleton, so that you can easily access it from other objects.

You will understand that a Model to be effective must be properly designed, but this is the way to go.

Check this tutorial and also this S.O. topic.

Upvotes: 1

Sirdek
Sirdek

Reputation: 86

I think the concept you need here is declared properties: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17

You seems to already using it (with @synthesize).

ivars needs getters and setters to be accessed from the exterior of the class. By using properties, these getters/setters can be automatically implemented with @synthesized.

For the application delegate, i do not understand: do you want to access another controller from the UIViewController via application delegate ?

Upvotes: 0

Related Questions