开发者

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?开发者_高级运维


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


Use the properties declared in each controller.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜