在目标c中全局访问实例变量

时间:2022-09-07 11:08:12

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.

我是iphone开发的新手。我想在另一个视图中访问一个视图中声明的变量。我怎样才能实现它。是否可以通过使用extern变量,如果是这样,如何声明和实现它。可以通过以下方式实现它使用委托?那么如何实现它。请指导我。我正在浏览谷歌得到和想法实现它,我想出了代表和外部变量,但我不知道如何实现或使用这些方法(委托,外部变量)请告诉我实现它的正确方法。谢谢。

1 个解决方案

#1


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

FirstView.h

@interface FirstView : UIView {
    NSString *data;
}
@property (nonatomic,copy) NSString *data;
@end

FirstView.m

FirstView.m

@implementation FirstView
// implement standard retain getter/setter for data:
@synthesize data;
@end

SecondView.m

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

#1


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

FirstView.h

@interface FirstView : UIView {
    NSString *data;
}
@property (nonatomic,copy) NSString *data;
@end

FirstView.m

FirstView.m

@implementation FirstView
// implement standard retain getter/setter for data:
@synthesize data;
@end

SecondView.m

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