2012-01-28 21 views
0

我有兩個視圖,view1調用:[self.view addSubview:view2.view];然後views2調用:[self.view removeFromSuperview];和我想重新加載view1中的數據當view1重新出現,但我不能調用方法或更新view1的屬性,因爲我無法在視圖2中創建#import "view1.h"(我在視圖1中創建了#import "view2.h")。從superview刷新日期

這是我的代碼:

View1.h:

-(void)reloadData; 

View1.m:

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

View2.h:

#import « View1.h » 

View2.m:

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

回答

0

如果您正確地重組文件,則可以在view2中導入view1,反之亦然。如果在.h文件中需要任何內容​​,則只需將#import "view1.h"置於view2.h中即可。如果你只需要在你的實現中需要這個,你可以在你的view2.m文件中愉快地移動#import "view1.h",從而解決循環依賴。

請注意,在許多情況下,如果僅用於創建類型的實例/參數,則可以跳過.h文件中的導入。例如

#import "Another.h" 

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

可改爲

@class Another; 

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

這基本上告訴編譯器,有一個叫Another的事情,但該細節現在並不重要。您可以在隨後的.m文件中稍後#import "Another.h"並像以前一樣工作。

+0

我無法調用[self.superview reloadData];在我看來2中,但view2的.m中的導入是很好的。 self.superview不起作用,爲什麼?它不能看到superview,只有self.subclass ... – 2012-01-28 16:49:25

+0

你可以添加一個屬性到'view2'鏈接回'view1':'@property(nonatomic,weak)View1 * callingView;'如果你不使用ARC)。您可以在view1中將其設置爲self,並在view2中提供'self.callingView'。 – 2012-01-28 17:03:16

+0

請確切地告訴我該怎麼做,謝謝。 – 2012-01-28 17:35:35