0

我有两个视图,view1 调用:[self.view addSubview:view2.view];然后是 views2 调用: [self.view removeFromSuperview];我想在 view1 重新出现时重新加载 view1 中的数据,但我无法调用方法或更新 view1 的属性,因为我无法#import "view1.h"在 view2 中创建(我'已经#import "view2.h"在view1)。

这是我的代码:

视图1.h:

-(void)reloadData;

视图1.m:

#import « View2.h » ; 
View2 *view2 = [[View2 alloc]init]; 
[self.view addSubview:view2.view]; 

视图2.h:

#import « View1.h » 

视图2.m:

// I want to call reloadData to reload Data of view1 before removing view2
[self.view removeFromSuperview];
4

1 回答 1

0

如果您正确地重新组织您的文件,您可以在 view2 中导入 view1,反之亦然。#import "view1.h"如果您需要 .h 文件中的任何内容,您只需将其放入 view2.h 中。如果您只在实现中需要它,您可以愉快地移入#import "view1.h"您的 view2.m 文件,从而解决循环依赖。

请注意,在许多情况下,如果只是为了创建类型的实例/参数,您可以跳过 .h 文件中的导入。例如

#import "Another.h"

@interface Onething
@property (strong, nonatomic) Another *an;
@end

可以改为

@class Another;

@interface Onething
@property (strong, nonatomic) Another *an;
@end

这基本上告诉编译器有一个东西叫做Another但细节现在并不重要。然后,您可以稍后#import "Another.h"在随附的 .m 文件中像以前一样工作。

于 2012-01-28T14:03:42.703 回答