GhostDZ9
GhostDZ9

Reputation: 155

passing object between two views (iOS SDK)

What is the best way to pass an object between two views and how would I go about doing so?

Upvotes: 0

Views: 2711

Answers (3)

PengOne
PengOne

Reputation: 48398

I'm assuming you have two view controllers, ViewController1 and ViewController2. In both header files (.h), add an instance variable:

CustomObject *myObjectToPass;

and also

@property (nonatomic, retain) CustomObject *myObjectToPass;

If you are passing a BOOL, int or float, then do not retain it, for an NSString, use copy in place of retain, etc.

In the implementation file (.m), synthesize the variable:

@synthesize myObjectToPass;

Now you can get and set the object between viewControllers. The best way to do this depends on how they are related (e.g. in a navigationController or a tabBarContoller, etc). This should get you started, though.

Upvotes: 0

saadnib
saadnib

Reputation: 11145

If you are using two view controllers then making property will be best way for you.

in .h file 

NSString *name;

@property (nonatomic, retain) NSString *name;

and in .m 

@synthesize name;

for more how to use property look - http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1

Upvotes: 1

arclight
arclight

Reputation: 5310

Use the properties declared in each controller.

Upvotes: 0

Related Questions