1

私は再びばかげているように感じますが、プロジェクトのサブビュー間を行き来することができません。先に進む限り、好きなだけサブビューを追加できますが、以前にアクセスしたビューの1つに戻りたいとすぐに問題が発生します...これが私の単純化されたコードです:

C4Workspace.h

#import "C4CanvasController.h"
#import "FirstView.h"
@interface C4WorkSpace : C4CanvasController{
    FirstView *firstView;
}
@end

C4Workspace.m

#import "C4Workspace.h"

@implementation C4WorkSpace

-(void)setup {
    firstView=[FirstView new];
    firstView.canvas.frame=CGRectMake(0, 0, self.canvas.width, self.canvas.height);
    firstView.canvas.userInteractionEnabled=YES;
    [firstView setup];
    firstView.mainCanvas=self.canvas;
    [self.canvas addSubview:firstView.canvas];
}

@end

FirstView.h

#import "C4CanvasController.h"
#import "SecondView.h"

@interface FirstView : C4CanvasController{
    C4Label *goToSecondView;
    SecondView *secondView;

}
@property (readwrite, strong) C4Window *mainCanvas;

@end

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];
    [self listenFor:@"touchesBegan" fromObject:goToSecondView andRunMethod:@"goToSecondView"];
}
-(void)goToSecondView{
    [goToSecondView removeFromSuperview];
    C4Log(@"second view");
    secondView=[SecondView new];
    secondView.canvas.frame=CGRectMake(0, 0, self.canvas.width, self.canvas.height);
    secondView.canvas.userInteractionEnabled=YES;
    [secondView setup];
    secondView.mainCanvas=self.canvas;
    [self.canvas addSubview:secondView.canvas];  
}
@end

SecondView.h

#import "C4CanvasController.h"
#import "FirstView.h"
@interface SecondView : C4CanvasController{
    C4Label *goToFirstView;
    FirstView *firstView;
}
@property (readwrite, strong) C4Window *mainCanvas;
@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];
    [self listenFor:@"touchesBegan" fromObject:goToFirstView andRunMethod:@"goToFirstView"];
}

-(void)goToFirstView{
    [goToFirstView removeFromSuperview];
    C4Log(@"first view");
    firstview=[FirstView new];
    firstview.canvas.frame=CGRectMake(0, 0, self.canvas.width, self.canvas.height);
    firstView.canvas.userInteractionEnabled=YES;
    [firstView setup];
    firstView.mainCanvas=self.canvas;
    [self.canvas addSubview:firstView.canvas];   
}
@end
4

1 に答える 1

1

手順を複雑にしすぎていると思います。

サブビューの「制御」は、含まれている View Controllerによって行われる必要があるため、2つのビューがキャンバス上にある場合、オブジェクト自体ではなく切り替えを処理するのはキャンバスである必要があります。

あなたのアプローチを改善するのに役立つこと:

  1. サブクラスからキャンバスへの参照を取り除きます。
  2. サブクラス化されたコントローラーのビューはすべてtouchesBegan通知を投稿します...これらをワークスペースで使用します[self listenFor:@"touchesBegan" fromObject:firstOrSecondView.canvas and runMethod:@"switch:"];
  3. zPosition適切なビューを変更するか、非表示/表示して切り替えます...

実装を次のように減らしました。

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

これは、ビューに異なる機能が必要な場合、たとえばFirstView内部機能が大きく異なる場合に機能SecondViewするはずです...それらが同じ場合は、それらを同じサブクラスに折りたたむことを検討する必要があります。

于 2013-10-23T19:32:57.800 に答える