开发者

Ascess the instance variable globally in objective c

I am new to iphone development.I want to access a variable declared in one view in another view.H开发者_运维技巧ow 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.


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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜