0

I've got two views, view1 calls : [self.view addSubview:view2.view]; then views2 calls: [self.view removeFromSuperview]; and I want to reload data in view1 when view1 reappear but I can't call a method or update a property of view1 because I can't make an#import "view1.h" in view2 (I've made an #import "view2.h" in view1).

This is my code :

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];
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

If you reorganize your files properly, you can import view1 in view2 and vice versa. You only need to put the #import "view1.h" in view2.h if you need any content right there in the .h file. If you only need this in your implementation, you can happily move #import "view1.h" in your view2.m file and thus resolve the circular dependency.

Note that in many cases you can skip importing in the .h file if this is only to create instances / paremeters of a type. For example

#import "Another.h"

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

can be changed to

@class Another;

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

This basically tells the compiler that there is a thing called Another but that the details are not important right now. You can then later #import "Another.h" in the accompanying .m file and work as before.

Dennis Bliefernicht
  • 5,147
  • 1
  • 28
  • 24
  • I can't call [self.superview reloadData]; in my view2 but the import is good in the .m of view2. self.superview doesn't work why ? It can't see superview, only self.subclass... – Jonathan Martin Jan 28 '12 at 16:49
  • You could add a property to `view2` that links back to `view1`: `@property (nonatomic, weak) View1 *callingView;` (replace weak by assign if you're not using ARC). You can set this in view1 to self and thus have `self.callingView` available in view2. – Dennis Bliefernicht Jan 28 '12 at 17:03
  • In view2.h declare a `@property (nonatomic, weak) View1 *callingView` (and add `@class View1;` above. When creating your view2 (in View1.m) you now can append `view2.callingView = self`. You now can `#import "View1.h"` in view2.m and do `[self.callingView reloadData]` there. – Dennis Bliefernicht Jan 29 '12 at 14:18
  • I think I've done it but I have an : EXC_BAD_ACCESS. This is my code : http://stackoverflow.com/questions/9055216/exit-bad-access – Jonathan Martin Jan 29 '12 at 17:49