So this is the code i have:
ViewA.h:
#import "ViewB.h"
#import "ViewC.h"
@interface ViewA : UIViewController {
ViewB *viewB;
ViewC *viewC;
}
@property(nonatomic,retain) ViewB *viewB;
@end
ViewA.m:
@synthesize viewB;
-(void)viewDidLoad {
viewB = [[ViewB alloc] initWithFrame:CGRectMake(0, 0, 320, 328)];
[self.view addSubview:viewB];
viewC = [[ViewC alloc] initWithFrame:CGRectMake(0, 332, 320, 84)];
[self.view addSubview:viewC];
}
ViewB.h:
-(void)methodToPass;
ViewB.m:
-(void)methodToPass {
NSLog(@"Passed.");
}
ViewC.h:
#import "ViewB.h"
@interface ViewC : UIScrollView {
ViewB *viewB;
}
-(void)methodWhichDoesGetPassed;
ViewC.m:
#import "ViewA.h"
@implementation ViewC
ViewA *viewA;
-(void)methodWhichDoesGetPassed {
NSLog(@"Test here");
[viewA.viewB methodToPass];
}
So the key bit of that is that i tried to pass the method to viewB, from viewC, through viewA in that last part. When i run the app, i get the “Test here” but not the “Passed”, so it doesn’t pass this through.
Any ideas how to fix this?
SinceviewCis a subview ofviewA, you should send messages to thesuperview.change it to
EDIT
You can even check if the superview is an instance of
ViewAbefore casting.EDIT2
Rather than having to message up to
viewAand down toviewBagain, wouldn’t it be better to passviewBtoviewCon instantiation. Suggesting some changes based on that.ViewA.m:
ViewC.h:
ViewC.m:
Original Answer