I am new to iphone development.I want to access a variable declared in one view in another view.How can i achieve it.Whether it is possible by using extern variable, if so how to declare and implement it.Can i achieve it by using delegates?then so how to implement it.Please guide me.I am browsing google to get and idea to achieve it, i came up with delegates and extern variable, but i dont know how implement or use these methods(delegates,extern variable).Please tell me the right way to achieve it.Thanks.
Asked
Active
Viewed 431 times
1 Answers
2
You could declare and implement a property on first view and set it from the second view.
This requires that the second view has a reference to the first view.
For example:
FirstView.h
@interface FirstView : UIView {
NSString *data;
}
@property (nonatomic,copy) NSString *data;
@end
FirstView.m
@implementation FirstView
// implement standard retain getter/setter for data:
@synthesize data;
@end
SecondView.m
@implementation SecondView
- (void)someMethod {
// if "myFirstView" is a reference to a FirstView object, then
// access its "data" object like this:
NSString *firstViewData = myFirstView.data;
}
@end

gerry3
- 21,420
- 9
- 66
- 74
-
@ implementation SecondView should i import firstView.h – Warrior Feb 11 '10 at 08:59
-
What is myFirstView.name,"name" refers to what? – Warrior Feb 11 '10 at 09:09
-
Yes, you need to import FirstView.h in SecondView.h or SecondView.m. Sorry, "name" should have been "data", it has been fixed. – gerry3 Feb 11 '10 at 09:24
-
1Hi, it's better to use copy instead of retain for NSString properties (this question explains why better than I ever could : http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain) – deanWombourne Feb 11 '10 at 09:27
-
Thanks. I figured someone would mention that. I don't typically use mutable strings. – gerry3 Feb 11 '10 at 18:24