I think you're over complicating the procedure.
The "control" of which subview should be done by the containing view controller, so, if the two views are on the canvas then it should be the canvas that deals with the switching instead of the objects themselves.
Thing to help improve your approach:
- Get rid of references to the canvas from the subclasses.
- The views of your subclassed controllers all post
touchesBegan
notifications... Use these in the workspace i.e.[self listenFor:@"touchesBegan" fromObject:firstOrSecondView.canvas and runMethod:@"switch:"];
- Switch by either changing the
zPosition
or hiding / revealing the appropriate view...
I reduced your implementations to the following:
FirstView.m
#import "FirstView.h"
@implementation FirstView
-(void)setup{
goToSecondView=[C4Label labelWithText:@"go to second View"];
goToSecondView.origin=CGPointMake(20, 50);
[self.canvas addLabel:goToSecondView];
goToSecondView.userInteractionEnabled = NO;
}
@end
SecondView.m
#import "SecondView.h"
@implementation SecondView
-(void)setup{
goToFirstView=[C4Label labelWithText:@"go to First View"];
goToFirstView.origin=CGPointMake(20, 50);
[self.canvas addLabel:goToFirstView];
goToFirstView.userInteractionEnabled = NO;
}
@end
C4WorkSpace.m
#import "C4Workspace.h"
#import "FirstView.h"
#import "SecondView.h"
@implementation C4WorkSpace {
FirstView *firstView;
SecondView *secondView;
}
-(void)setup {
firstView=[FirstView new];
firstView.canvas.frame = self.canvas.frame;
[firstView setup];
firstView.canvas.userInteractionEnabled = YES;
[self.canvas addSubview:firstView.canvas];
secondView=[SecondView new];
secondView.canvas.frame = self.canvas.frame;
[secondView setup];
secondView.canvas.userInteractionEnabled = YES;
[self.canvas addSubview:secondView.canvas];
secondView.canvas.hidden = YES;
[self listenFor:@"touchesBegan"
fromObjects:@[firstView.canvas, secondView.canvas]
andRunMethod:@"switchView:"];
}
-(void)switchView:(NSNotification *)aNotification {
C4View *v = (C4View *)aNotification.object;
if([v isEqual:firstView.canvas]) {
firstView.canvas.hidden = YES;
secondView.canvas.hidden = NO;
} else {
firstView.canvas.hidden = NO;
secondView.canvas.hidden = YES;
}
}
@end
This should work for you in the case that your views need differing functionality, like FirstView
will have significantly different internal functionality than SecondView
... If they are the same then you should think about collapsing them into the same subclass.