views:

64

answers:

1

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.

+2  A: 

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
@ implementation SecondView should i import firstView.h
Warrior
What is myFirstView.name,"name" refers to what?
Warrior
Yes, you need to import FirstView.h in SecondView.h or SecondView.m. Sorry, "name" should have been "data", it has been fixed.
gerry3
Hi, 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
Thanks. I figured someone would mention that. I don't typically use mutable strings.
gerry3
Thanks friend . It worked fine.
Warrior