1

我有一个名为“系统”的类,它包含一些变量和两个数组,我需要从其他两个类访问它,这两个类应该能够读取和写入这些变量我是一个完全的初学者,所以很可能我已经犯了一些错误。

系统.h

@interface System : UIViewController{

float length_of_one_hour; 
float length_of_first_break;
float length_of_second_break;
float length_of_lunch_break;
float length_of_shortned_hour;
float school_begin;

int school_end[5];
float school_length[5]; 

}

About_now.m

- (void)read_school_end_monday{

school_end_label.text=[NSString stringWithFormat:@"%i", school_end[0]];

}

设置.m

- (IBAction)set_school_end_monday{

school_end[0]= [school_end_on_mondays_textfield.text intValue];

}

但我不知道在 System.h 和 About_now.m 中写什么,变量保存在 System 类中,可以从任何地方访问。是的,我已经尝试过@public 和 extern。顺便说一句,我需要为 school_end 设置一个数组,因为我将使用一个已经有效的函数来计算它(使用一个小时的长度以及学校实际开始的时间等),但我需要在之后访问 About_now 类中的变量.

希望有人可以帮助我。谢谢

4

4 回答 4

0

你有很多设计选项可以做到这一点:

  1. 创建一个全局System对象;

  2. 使System课程成为Singleton

  3. 定义System类接口,以便它在类级别(相对于对象级别)为其“属性”提供访问器方法。

  4. System像现在一样实例化并将其作为初始化参数传递给其他两个类。

现在,更详细地说:

(1) 声明:

     extern System* globalSystemObject;

System.h; 然后定义:

static System* globalSystemObject = nil;

System.m. 不要忘记globalSystemObject在代码中的某处进行初始化:

globalSystemObject = [[System alloc] init];

这样做,您可以globalSystemObject从任何导入的文件中使用System.h.

(2) 在这里查看有关实现单例类的详细信息。请记住,许多人建议不要使用 Singleton(如果它只是“隐藏”全局变量的一种方式)。在这种特定情况下,考虑到名为 System 的类的语义,我会说 Singleton 是您的最佳选择。

(3) 您的班级可能如下所示:

    @interface System : UIViewController{
    }
    +(float)length_of_one_hour;
    +(void)set_length_of_one_hour:(float)length;

    +(float)length_of_first_break;
    +(void)length_of_first_break:(float)length;

    ...
    @end

您可以通过 1 中的静态变量在 .m 文件中实现这些变量。

(4) 这很简单。唯一的事情是您必须System在同一范围内创建实例和其他两个类的实例:

System* system = [[System alloc] init];
about_now* anow = [[about_now alloc] initWithSystemInstance:system];
...

system参数将存储在about_now.

这样做可以完全避免使用全局变量(即使是那些隐藏在 Singleton 掩码后面的变量)。

于 2012-04-26T20:05:32.797 回答
0

您需要一个指向 System.Instance 实例的指针。所以要访问成员变量:

System* system = GetSystem();
system->school_end[0] = whatever();

这与通过结构指针访问成员完全相同。

但话说回来,通过方法隐藏访问是一个好习惯:

- (int)getSchoolEndAtIndex:(int)index
{
    return school_end[index];
}
于 2012-04-26T20:06:33.587 回答
0

在 iOS 应用程序中跨类共享数据的常用方法是遵循单例模式。

系统模型.h

#import <Foundation/Foundation.h>

@interface SystemModel : NSObject {
    @public
    float length_of_one_hour; 
    float length_of_first_break;
    float length_of_second_break;
    float length_of_lunch_break;
    float length_of_shortned_hour;
    float school_begin;

    int school_end[5];
    float school_length[5]; 
}
+(SystemModel*)instance;
@end

系统模型.m

@implementation SystemModel
static SystemModel* _instance= nil;

+(SystemModel*)instance {
    @synchronized([SystemModel class]) {
        if (!_instance)
            [[self alloc] init];
        return _instance;
    }
    return nil;
}

+(id)alloc {
    @synchronized([SystemModel class]) {
        return (_instance = [super alloc]);
}
    return nil;
}

-(id)init {
    self = [super init];
    if (self != nil) {
        // your init code
    }
    return self;
}

@end

现在您可以像这样使用您的实例:

 float tmp = [SystemModel instance]->length_of_one_hour;

您还可以将实例变量转换为属性,并使用点语法。浮点数和数组无关紧要,但对于id使用属性的基于对象是首选。

于 2012-04-26T20:10:22.837 回答
0

您可以通过 getters/setters (@property / @synthesize) 公开系统实例变量,并将引用传递给其他类实例。

@接口系统:UIViewController{

浮动长度_of_one_hour;//... @property (nonatomic,assign); .. @结尾

OtherClass other = [[OtherClass alloc] initWithSystem:sys]; 或使用 setter [其他 setSystem:sys];

于 2012-04-26T21:04:30.270 回答